Skip to content

Instantly share code, notes, and snippets.

@thuydao
Last active January 25, 2021 09:10
Show Gist options
  • Select an option

  • Save thuydao/0bdf55de68351536a6732cdaf9e7c73a to your computer and use it in GitHub Desktop.

Select an option

Save thuydao/0bdf55de68351536a6732cdaf9e7c73a to your computer and use it in GitHub Desktop.
go_mac_adr
package main
import (
"bytes"
"strconv"
"net"
)
func main() {
print(macAdrr())
}
func macAdrr() string {
interfaces, err := net.Interfaces()
if err != nil {
return ""
}
for _, i := range interfaces {
if i.Flags&net.FlagUp != 0 && bytes.Compare(i.HardwareAddr, nil) != 0 {
// Skip locally administered addresses
if i.HardwareAddr[0]&2 == 2 {
continue
}
var mac uint64
for j, b := range i.HardwareAddr {
if j >= 8 {
break
}
mac <<= 8
mac += uint64(b)
}
return strconv.FormatUint(mac, 16)
}
}
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment