Skip to content

Instantly share code, notes, and snippets.

@johnsonz
Created May 20, 2016 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnsonz/caeee9380f08f9a7eed20dacf1c025c7 to your computer and use it in GitHub Desktop.
Save johnsonz/caeee9380f08f9a7eed20dacf1c025c7 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