xxxxxxxxxx
package utils
import (
"net"
)
func LookupIp() (map[string]interface{}, error) {
var res map[string]interface{} = make(map[string]interface{})
address, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, v := range address {
ipnet, _ := v.(*net.IPNet)
if v4 := ipnet.IP.To4(); v4 != nil {
res["ipv4"] = v4.String()
continue
}
if v6 := ipnet.IP.To16(); v6 != nil {
res["ipv6"] = v6.String()
continue
}
break
}
return res, nil
}
xxxxxxxxxx
// GetLocalIP returns the non loopback local IP of the host
func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}