github.com/simpleiot/simpleiot@v0.18.3/network/ip.go (about) 1 package network 2 3 import ( 4 "errors" 5 "net" 6 ) 7 8 // GetIP returns the IP address for the itnerface 9 func GetIP(ifaceName string) (string, error) { 10 iface, err := net.InterfaceByName(ifaceName) 11 if err != nil { 12 return "", err 13 } 14 15 addrs, err := iface.Addrs() 16 if err != nil { 17 return "", err 18 } 19 20 for _, addr := range addrs { 21 switch v := addr.(type) { 22 case *net.IPNet: 23 if !v.IP.IsLoopback() && v.IP.To4() != nil { 24 return addr.String(), nil 25 } 26 } 27 } 28 29 return "", errors.New("No IP address") 30 }