Last active
May 13, 2019 02:21
-
-
Save ialex32x/b94027d5716139fbae5d7aed09f75c64 to your computer and use it in GitHub Desktop.
how to iterate net interfaces in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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