gateway/utils/ip_utils.go
李寻欢 21950f5dbf
All checks were successful
continuous-integration/drone/push Build is passing
🎨 完善网关
2021-09-09 09:48:58 +08:00

26 lines
470 B
Go

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)
}
}
}
//ipStr := strings.Join(ips, ",")
return ips
}