Skip to content

Instantly share code, notes, and snippets.

@M2shad0w
Forked from johnsonz/go-get-ip-from-request
Created June 11, 2018 01:41
Show Gist options
  • Save M2shad0w/12e5028a0eceecee5e810a49ef1f4989 to your computer and use it in GitHub Desktop.
Save M2shad0w/12e5028a0eceecee5e810a49ef1f4989 to your computer and use it in GitHub Desktop.
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