package main import ( "fmt" "net" "os" ) func main() { addrs, err := net.InterfaceAddrs() if err != nil { fmt.Println(err) os.Exit(1) } for _, address := range addrs { // 检查ip地址判断是否回环地址 if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { if ipnet.IP.To4() != nil { fmt.Println(ipnet.IP.String()) } } } }
可以试试这个:https://www.cnblogs.com/chaselogs/p/11301940.html,亲测可用
#5