mirror of
https://github.com/unti-io/go-utils.git
synced 2025-10-05 08:16:50 +08:00
v1.2.0
This commit is contained in:
@@ -43,6 +43,7 @@ func init() {
|
||||
Is.MapAny = IsMapAny
|
||||
Get.Type = GetType
|
||||
Get.Ip = GetIp
|
||||
Get.Mac = GetMac
|
||||
In.Array = InArray[any]
|
||||
Array.Filter = ArrayFilter
|
||||
Array.Remove = ArrayRemove
|
||||
@@ -121,6 +122,7 @@ var Is struct {
|
||||
var Get struct {
|
||||
Type func(value any) (result string)
|
||||
Ip func(key ...string) (result any)
|
||||
Mac func() (result string)
|
||||
Resolution func(index int) (size int)
|
||||
}
|
||||
|
||||
|
@@ -24,6 +24,17 @@ func Ternary[T any](IF bool, TRUE T, FALSE T) T {
|
||||
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 - 字符串替换
|
||||
func Replace(value any, params map[string]any) (result string) {
|
||||
result = cast.ToString(value)
|
||||
|
32
utils/get.go
32
utils/get.go
@@ -2,6 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -42,7 +43,7 @@ func GetIp(key ...string) (result any) {
|
||||
wr.Data["intranet"] = localAddr[0:idx]
|
||||
wr.Lock.Unlock()
|
||||
}()
|
||||
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
// 外网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
|
||||
}
|
||||
}
|
||||
|
||||
// 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 ""
|
||||
}
|
||||
|
Reference in New Issue
Block a user