gateway/utils/ip_utils.go

26 lines
470 B
Go
Raw Normal View History

2021-09-07 13:40:44 +08:00
package utils
import (
"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()
ips = append(ips, ip)
}
}
}
2021-09-09 09:48:58 +08:00
//ipStr := strings.Join(ips, ",")
2021-09-07 13:40:44 +08:00
return ips
}