~~~
测试了如下都不成功 得不到访问者客户的IP地址
fmt.Println(r.RemoteAddr)
fmt.Println(r.Header.get("Remote_addr"))
请问还有什么函数可以获取 客户端IP呢。
~~~
更多评论
```go
func RemoteIp(req *http.Request) string {
remoteAddr := req.RemoteAddr
if ip := req.Header.Get(XRealIP); ip != "" {
remoteAddr = ip
} else if ip = req.Header.Get(XForwardedFor); ip != "" {
remoteAddr = ip
} else {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
}
if remoteAddr == "::1" {
remoteAddr = "127.0.0.1"
}
return remoteAddr
}
```
#2