Last active
October 31, 2022 03:42
-
-
Save johnsonz/8694c0c16610dc2190e56da5988dcb27 to your computer and use it in GitHub Desktop.
go获取本机IP地址,包括IPv4和IPv6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func GetLocalIP() { | |
host, _ := os.Hostname() | |
addrs, _ := net.LookupIP(host) | |
for _, addr := range addrs { | |
if ipv4 := addr.To4(); ipv4 != nil { | |
//return ipv4.String() | |
} | |
} | |
//return "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这个根本什么也获取不到啊,是nil