~~~
测试了如下都不成功 得不到访问者客户的IP地址
fmt.Println(r.RemoteAddr)
fmt.Println(r.Header.get("Remote_addr"))
请问还有什么函数可以获取 客户端IP呢。
~~~
<a href="/user/polaris" title="@polaris">@polaris</a>
你这个编译报错 有些是自定义函数吧
~~~
.\main.go:34:26: undefined: XRealIP
.\main.go:36:32: undefined: XForwardedFor
.\main.go:39:22: undefined: net
~~~
#3
更多评论
```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