28 lines
498 B
Go
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
|
||
|
}
|