-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
51 lines (42 loc) · 987 Bytes
/
net.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
41
42
43
44
45
46
47
48
49
50
51
package mygo
import (
"errors"
"net"
)
// LocalIP 返回第一个本地 IP 地址
func (*GoNet) LocalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, address := range addrs {
if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
return ipNet.IP.String(), nil
}
}
}
return "", errors.New("can't get local IP")
}
// LocalMac 获取本地MAC地址
func (*GoNet) LocalMac() (string, error) {
interfaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, inter := range interfaces {
address, err := inter.Addrs()
if err != nil {
return "", err
}
for _, addr := range address {
// 检测是否是回环地址127.0.0.1 (::1)
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
return inter.HardwareAddr.String(), nil
}
}
}
}
return "", errors.New("can't get local mac")
}