26 lines
470 B
Go
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
|
|
}
|