Skip to content

Instantly share code, notes, and snippets.

@johnsonz
Last active October 31, 2022 03:42
Show Gist options
  • Save johnsonz/8694c0c16610dc2190e56da5988dcb27 to your computer and use it in GitHub Desktop.
Save johnsonz/8694c0c16610dc2190e56da5988dcb27 to your computer and use it in GitHub Desktop.
go获取本机IP地址,包括IPv4和IPv6
func GetLocalIP() {
host, _ := os.Hostname()
addrs, _ := net.LookupIP(host)
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
//return ipv4.String()
}
}
//return ""
}
@FBIanderson
Copy link

这个根本什么也获取不到啊,是nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment