github.com/duomi520/utils@v0.0.0-20240430123446-e03a4cddd6ec/ip.go (about) 1 package utils 2 3 import "net" 4 5 //PrivateIP4 本地IP4地址 6 func PrivateIP4() string { 7 addrs, err := net.InterfaceAddrs() 8 if err != nil { 9 return "" 10 } 11 for _, address := range addrs { 12 if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { 13 if ipNet.IP.To4() != nil { 14 if ipNet.IP.IsPrivate() { 15 return ipNet.IP.String() 16 } 17 } 18 } 19 } 20 return "" 21 }