mirror of
https://github.com/unti-io/go-utils.git
synced 2025-10-06 16:57:35 +08:00
v1.2.0
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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)
|
||||||
|
28
utils/get.go
28
utils/get.go
@@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/spf13/cast"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -63,3 +64,30 @@ 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 ""
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user