From be180a401e056d621227d2a3e7ba8f3530438b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Tue, 9 Mar 2021 17:59:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E6=9C=AC?= =?UTF-8?q?=E6=9C=BAIP=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/ip.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 util/ip.go diff --git a/util/ip.go b/util/ip.go new file mode 100644 index 0000000..960f1a8 --- /dev/null +++ b/util/ip.go @@ -0,0 +1,29 @@ +// Package util... +// +// Description : util... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-03-09 5:56 下午 +package util + +import "net" + +// GetHostIP 获取本机IP地址 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:58 下午 2021/3/9 +func GetHostIP() string { + hostIP := "127.0.0.1" + addrs, _ := net.InterfaceAddrs() + for _, address := range addrs { + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + hostIP = ipnet.IP.String() + break + } + } + } + return hostIP +}