/
gipv4_mac.go
40 lines (36 loc) · 923 Bytes
/
gipv4_mac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 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"
)
// 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 {
return nil, err
}
for _, netInterface := range netInterfaces {
macAddr := netInterface.HardwareAddr.String()
if len(macAddr) == 0 {
continue
}
macs = append(macs, macAddr)
}
return macs, nil
}