github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/net/ip.go (about)

     1  package net
     2  
     3  import "net"
     4  
     5  func GetLocalIPv4() string {
     6  	addrs, err := net.InterfaceAddrs()
     7  	if err != nil {
     8  		return ""
     9  	}
    10  	for _, addr := range addrs {
    11  		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
    12  			if ipnet.IP.To4() != nil {
    13  				return ipnet.IP.String()
    14  			}
    15  		}
    16  	}
    17  	return ""
    18  }
    19