14

Is there an easy way to get the permanent MAC Address using Go?

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Simone Romani
  • 415
  • 2
  • 6
  • 20

1 Answers1

23
package main

import (
    "fmt"
    "log"
    "net"
)

func getMacAddr() ([]string, error) {
    ifas, err := net.Interfaces()
    if err != nil {
        return nil, err
    }
    var as []string
    for _, ifa := range ifas {
        a := ifa.HardwareAddr.String()
        if a != "" {
            as = append(as, a)
        }
    }
    return as, nil
}

func main() {
    as, err := getMacAddr()
    if err != nil {
        log.Fatal(err)
    }
    for _, a := range as {
        fmt.Println(a)
    }
}
mattn
  • 7,571
  • 30
  • 54