github.com/gogf/gf@v1.16.9/net/gipv4/gipv4_mac.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 // 7 8 package gipv4 9 10 import ( 11 "net" 12 ) 13 14 // GetMac retrieves and returns the first mac address of current host. 15 func GetMac() (mac string, err error) { 16 macs, err := GetMacArray() 17 if err != nil { 18 return "", err 19 } 20 if len(macs) > 0 { 21 return macs[0], nil 22 } 23 return "", nil 24 } 25 26 // GetMacArray retrieves and returns all the mac address of current host. 27 func GetMacArray() (macs []string, err error) { 28 netInterfaces, err := net.Interfaces() 29 if err != nil { 30 return nil, err 31 } 32 for _, netInterface := range netInterfaces { 33 macAddr := netInterface.HardwareAddr.String() 34 if len(macAddr) == 0 { 35 continue 36 } 37 macs = append(macs, macAddr) 38 } 39 return macs, nil 40 }