package main
import "fmt"
import "net"
const inter = "en1" // use eth0 for linux
func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
ifi, err := net.InterfaceByName(inter)
check(err)
addrs, err := ifi.Addrs()
check(err)
for _, a := range addrs {
fmt.Printf("Interface %q, address %v\n", ifi.Name, a)
}
}
// outputs
// Interface "en1", address fe80::2a37:37ff:fe12:10a9/64
// Interface "en1", address 192.168.1.129/24
有疑问加站长微信联系(非本文作者)