github.com/niubaoshu/goutils@v0.0.0-20180828035119-e8e576f66c2b/net/getLocalIPAddrs.go (about) 1 package net 2 3 import ( 4 "net" 5 ) 6 7 func GetLocalIPAddrs() (ips []net.IP, err error) { 8 addrs, err := net.InterfaceAddrs() 9 if err != nil { 10 return ips, err 11 } 12 for _, addr := range addrs { 13 if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && !ipnet.IP.IsUnspecified() && ipnet.IP.To4() != nil { 14 ips = append(ips, ipnet.IP) 15 } 16 } 17 return 18 }