gateway/utils/ip_utils.go
2021-09-07 13:40:44 +08:00

28 lines
498 B
Go

package utils
import (
"fmt"
"net"
)
// GetIps 获取本机IP地址
func GetIps() []string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil
}
var ips []string
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip := ipnet.IP.String()
fmt.Println(ip)
ips = append(ips, ip)
}
}
}
//ipStr := strings.Join(aaaa, ",")
return ips
}