Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
go从Request获取IP地址
func GetIPFromRequest(r *http.Request) string {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
glog.Errorf("userip: %q is not IP:port", r.RemoteAddr)
return ""
}
userIP := net.ParseIP(ip)
if userIP == nil {
glog.Errorf("userip: %q is not IP:port", r.RemoteAddr)
return ""
}
return userIP.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment