~~~
测试了如下都不成功 得不到访问者客户的IP地址
fmt.Println(r.RemoteAddr)
fmt.Println(r.Header.get("Remote_addr"))
请问还有什么函数可以获取 客户端IP呢。
~~~
<a href="/user/polaris" title="@polaris">@polaris</a> 可否补全下呢 上面的报错 可能还有变量或则函数 没发出来呢
#4
更多评论
```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