Skip to content

Instantly share code, notes, and snippets.

@ialex32x
Last active May 13, 2019 02:21
Show Gist options
  • Save ialex32x/b94027d5716139fbae5d7aed09f75c64 to your computer and use it in GitHub Desktop.
Save ialex32x/b94027d5716139fbae5d7aed09f75c64 to your computer and use it in GitHub Desktop.
how to iterate net interfaces in golang
/*
go 获取所有网卡地址
*/
package main
import (
"fmt"
"net"
)
func main() {
addrs, err := net.InterfaceAddrs()
if err != nil {
return
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok {
if ipnet.IP.IsLoopback() ||
ipnet.IP.IsLinkLocalMulticast() ||
ipnet.IP.IsLinkLocalUnicast() {
continue
}
fmt.Printf("%v\n", addr)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment