This commit is contained in:
兔子
2023-06-13 18:07:55 +08:00
parent 6c9aef2090
commit f760f1f0a9
3 changed files with 43 additions and 2 deletions

View File

@@ -43,6 +43,7 @@ func init() {
Is.MapAny = IsMapAny Is.MapAny = IsMapAny
Get.Type = GetType Get.Type = GetType
Get.Ip = GetIp Get.Ip = GetIp
Get.Mac = GetMac
In.Array = InArray[any] In.Array = InArray[any]
Array.Filter = ArrayFilter Array.Filter = ArrayFilter
Array.Remove = ArrayRemove Array.Remove = ArrayRemove
@@ -121,6 +122,7 @@ var Is struct {
var Get struct { var Get struct {
Type func(value any) (result string) Type func(value any) (result string)
Ip func(key ...string) (result any) Ip func(key ...string) (result any)
Mac func() (result string)
Resolution func(index int) (size int) Resolution func(index int) (size int)
} }

View File

@@ -24,6 +24,17 @@ func Ternary[T any](IF bool, TRUE T, FALSE T) T {
return FALSE return FALSE
} }
// Default - 设置默认值
func Default[T any](param T, value ...T) (result T) {
if IsEmpty(param) {
if len(value) > 0 {
return value[0]
}
return result
}
return param
}
// Replace - 字符串替换 // Replace - 字符串替换
func Replace(value any, params map[string]any) (result string) { func Replace(value any, params map[string]any) (result string) {
result = cast.ToString(value) result = cast.ToString(value)

View File

@@ -2,6 +2,7 @@ package utils
import ( import (
"fmt" "fmt"
"github.com/spf13/cast"
"net" "net"
"strings" "strings"
"sync" "sync"
@@ -42,7 +43,7 @@ func GetIp(key ...string) (result any) {
wr.Data["intranet"] = localAddr[0:idx] wr.Data["intranet"] = localAddr[0:idx]
wr.Lock.Unlock() wr.Lock.Unlock()
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
// 外网IP - 替代品https://api.ipify.org https://ipinfo.io/ip https://api.ip.sb/ip // 外网IP - 替代品https://api.ipify.org https://ipinfo.io/ip https://api.ip.sb/ip
@@ -62,4 +63,31 @@ func GetIp(key ...string) (result any) {
} }
return wr.Data return wr.Data
} }
// GetMac - 获取本机MAC
func GetMac() (result string) {
interfaces, err := net.Interfaces()
if err != nil {
return ""
}
for _, item := range interfaces {
// 过滤掉非物理接口类型
if item.Flags&net.FlagUp != 0 && item.Flags&net.FlagLoopback == 0 && item.Flags&net.FlagPointToPoint == 0 {
if value, err := item.Addrs(); err == nil {
for _, val := range value {
if IPNet, ok := val.(*net.IPNet); ok && !IPNet.IP.IsLoopback() {
if mac := item.HardwareAddr; len(mac) > 0 {
return cast.ToString(mac)
}
}
}
}
}
}
return ""
}