golang获取本地ip以及MAC
//获取本机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 ""
}