下面是网上一个获取本机 ip 的一个程序,程序中 for 遍历的时候有一句话 `address.(*net.IPNet)`,不知道如何理解,请教大佬们
```
addrs, err := net.InterfaceAddrs()
if err != nil {
log.Fatalln("Error: ", err)
}
log.Println(addrs)
log.Printf("%T", addrs)
log.Printf("%#v", addrs)
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
log.Println(ipnet.IP.String())
}
}
}
```
```
log.Println(addrs)
log.Printf("%T", addrs)
log.Printf("%#v", addrs)
```
上面的输出结果对应如下:
```
2019/10/04 14:27:58 [fe80::ec48:11d9:86ac:7c54/64 192.168.1.113/24 fe80::995d:14ef:8349:bc9f/64 169.254.188.159/16 fe80::981:65dc:f28b:6c9a/64 169.254.108.154/16 fe80::f936:3746:4559:6e54/64 169.254.110.84/16 192.168.137.1/24 fe80::e9da:aee7:6f5c:db0a/64 169.254.219.10/16 ::1/128 127.0.0.1/8]
2019/10/04 14:27:58 []net.Addr
2019/10/04 14:27:58 []net.Addr{(*net.IPNet)(0xc0000c2000), (*net.IPNet)(0xc0000c2030), (*net.IPNet)(0xc0000c2060), (*net.IPNet)(0xc0000c2090), (*net.IPNet)(0xc0000c20c0), (*net.IPNet)(0xc0000c20f0), (*net.IPNet)(0xc0000c2120), (*net.IPNet)(0xc0000c2150), (*net.IPNet)(0xc0000c2180), (*net.IPNet)(0xc0000c21b0), (*net.IPNet)(0xc0000c21e0), (*net.IPNet)(0xc0000c2210), (*net.IPNet)(0xc0000c2240)}
```
有疑问加站长微信联系(非本文作者)