假设本机只有一个ip和mac,多个可用[]string 实现
-
//获取本机ip
-
func GetLocalIp() string {
-
addrs, err := net.InterfaceAddrs()
-
if err != nil {
-
slog.Debug("get local ip failed")
-
}
-
for _, address := range addrs {
-
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
-
if ipnet.IP.To4() != nil {
-
return ipnet.IP.String()
-
}
-
}
-
}
-
return ""
-
}
-
-
//获取本机Mac
-
func GetMac() string {
-
interfaces, err := net.Interfaces()
-
if err != nil {
-
slog.Debug("Get loacl Mac failed")
-
}
-
for _, inter := range interfaces {
-
mac := inter.HardwareAddr
-
slog.Debug("+======" + mac.String())
-
if mac.String() != "" {
-
return mac.String()
-
}
-
-
}
-
return ""
-
}
阅读(5599) | 评论(0) | 转发(0) |