You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-library/vendor/github.com/gogf/gf/v2/net/gipv4/gipv4_mac.go

44 lines
1013 B

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
//
package gipv4
import (
"net"
"github.com/gogf/gf/v2/errors/gerror"
)
// GetMac retrieves and returns the first mac address of current host.
func GetMac() (mac string, err error) {
macs, err := GetMacArray()
if err != nil {
return "", err
}
if len(macs) > 0 {
return macs[0], nil
}
return "", nil
}
// GetMacArray retrieves and returns all the mac address of current host.
func GetMacArray() (macs []string, err error) {
netInterfaces, err := net.Interfaces()
if err != nil {
err = gerror.Wrap(err, `net.Interfaces failed`)
return nil, err
}
for _, netInterface := range netInterfaces {
macAddr := netInterface.HardwareAddr.String()
if len(macAddr) == 0 {
continue
}
macs = append(macs, macAddr)
}
return macs, nil
}