From e54387822ffafd07b7994bd4fe598e42365bfac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sun, 15 May 2022 21:33:41 +0800 Subject: [PATCH] update --- README.md | 19 +++++++++++++++++++ ip.go | 17 +++++++++++++++++ ip_test.go | 4 ++++ 3 files changed, 40 insertions(+) diff --git a/README.md b/README.md index 1473fd1..c78fc68 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,23 @@ go get -v -u go.dtapp.net/goip import ( "go.dtapp.net/goip" ) +``` + +```go +package main + +import ( + "go.dtapp.net/goip" + "testing" +) + +func TestGoIp(t *testing.T) { + // 获取Mac地址 + t.Log(goip.GetMacAddr()) + // 内网ip + t.Log(goip.GetInsideIp()) + // 外网ip + t.Log(goip.GetOutsideIp()) +} + ``` \ No newline at end of file diff --git a/ip.go b/ip.go index 7d368c9..5689de1 100644 --- a/ip.go +++ b/ip.go @@ -71,3 +71,20 @@ func GetOutsideIp() (ip string) { ip = respGetOutsideIp.Data.Ip return respGetOutsideIp.Data.Ip } + +// GetMacAddr 获取Mac地址 +func GetMacAddr() (arrays []string) { + netInterfaces, err := net.Interfaces() + if err != nil { + return arrays + } + for _, netInterface := range netInterfaces { + addr := netInterface.HardwareAddr.String() + if len(addr) == 0 { + continue + } + + arrays = append(arrays, addr) + } + return arrays +} diff --git a/ip_test.go b/ip_test.go index 7b94663..4f05f6b 100644 --- a/ip_test.go +++ b/ip_test.go @@ -13,3 +13,7 @@ func TestGetInsideIp(t *testing.T) { func TestIps(t *testing.T) { t.Log(Ips()) } + +func TestGetMacAddr(t *testing.T) { + t.Log(GetMacAddr()) +}