![image.png](https://static.studygolang.com/180327/58319465d7a7806c992ac696b0e2bb99)
上图代码,下图结果
![image.png](https://static.studygolang.com/180327/9bf189cb5022a3778c25b1888c3232fa)
大神,告诉我正确姿势
```go
const (
XForwardedFor = "X-Forwarded-For"
XRealIP = "X-Real-IP"
)
// RemoteIp 返回远程客户端的 IP,如 192.168.1.1
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
}
```
供参考
#1