-
-
Save M2shad0w/12e5028a0eceecee5e810a49ef1f4989 to your computer and use it in GitHub Desktop.
go从Request获取IP地址
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 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