28 lines
465 B
Go
28 lines
465 B
Go
package ipv4
|
|
|
|
import (
|
|
"net"
|
|
)
|
|
|
|
// LocalIPs return all non-loopback IPv4 addresses
|
|
func LocalIPv4s() ([]string, error) {
|
|
var ips []string
|
|
addrs, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
return ips, err
|
|
}
|
|
|
|
for _, a := range addrs {
|
|
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
|
|
ips = append(ips, ipnet.IP.String())
|
|
}
|
|
}
|
|
|
|
return ips, nil
|
|
}
|
|
|
|
func LocalIP() string {
|
|
ips, _ := LocalIPv4s()
|
|
return ips[0]
|
|
}
|