Skip to content

Instantly share code, notes, and snippets.

@liasica
Created March 20, 2018 02:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save liasica/f4db81e8138b5b0d7978e4e55933914a to your computer and use it in GitHub Desktop.
Save liasica/f4db81e8138b5b0d7978e4e55933914a to your computer and use it in GitHub Desktop.
a simple example of how to grab a mac address in Golang.
// 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
}
@Sagleft
Copy link

Sagleft commented Nov 6, 2023

invalid operation: ifa.Flags & net.FlagUp != 0 && bytes.Compare(ifa.HardwareAddr, nil) (mismatched types untyped bool and int)

@liasica
Copy link
Author

liasica commented Nov 7, 2023

invalid operation: ifa.Flags & net.FlagUp != 0 && bytes.Compare(ifa.HardwareAddr, nil) (mismatched types untyped bool and int)

bytes.Compare(i.HardwareAddr, nil) != 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment