func GetLocalIP() {
host, _ := os.Hostname()
addrs, _ := net.LookupIP(host)
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
fmt.Println("IPv4: ", ipv4)
}
}
fmt.Println("----------------------------------------------------")
ifaces, err := net.Interfaces()
if err != nil {
return
}
// handle err
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
continue
}
// handle err
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
{
ip = v.IP
fmt.Println(ip.String())
}
case *net.IPAddr:
{
ip = v.IP
fmt.Println(ip.String())
}
}
// process IP address
}
}
}