generated from VLADIMIR/template
add local ip
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package network_utils
|
||||
|
||||
import "net"
|
||||
|
||||
// GetLocalIPv4Addresses returns non-loopback IPv4 addresses of the host.
|
||||
func GetLocalIPv4Addresses() ([]string, error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []string
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
switch addr := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = addr.IP
|
||||
case *net.IPAddr:
|
||||
ip = addr.IP
|
||||
}
|
||||
if ip == nil || ip.IsLoopback() || ip.To4() == nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, ip.String())
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user