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()) } } } }
你这把一堆都弄出来了把?
```golang
func getLocalIP() net.IP {
raddr, err1 := net.ResolveIPAddr("ip4:icmp", "220.181.38.148")
laddr, err2 := net.ResolveIPAddr("ip4:icmp", "")
con, err3 := net.DialIP("ip4:icmp", laddr, raddr)
if err1 != nil || err2 != nil || err3 != nil {
fmt.Println(err1, err2, err3)
return nil
}
defer con.Close()
return net.ParseIP(con.LocalAddr().String())
}
}
```
#6