add local ip

This commit is contained in:
2026-09-21 21:10:25 +07:00
parent bdf48f4e99
commit aa4b2f5d48
2 changed files with 41 additions and 0 deletions
@@ -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
}