Created
August 6, 2018 11:08
-
-
Save shangzongyu/ea667c9802d12271f5d26a98dc2683d2 to your computer and use it in GitHub Desktop.
a simple example of how to grab a mac address 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
| // getMacAddr gets the MAC hardware | |
| // address of the host machine | |
| func getMacAddr() (addr string) { | |
| interfaces, err := net.Interfaces() | |
| if err == nil { | |
| for _, i := range interfaces { | |
| if i.Flags&net.FlagUp != 0 && bytes.Compare(i.HardwareAddr, nil) != 0 { | |
| // Don't use random as we have a real address | |
| addr = i.HardwareAddr.String() | |
| break | |
| } | |
| } | |
| } | |
| return | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment