beego如何获取客户端IP?

来源:互联网 发布:如何自学软件编程 编辑:程序博客网 时间:2024/05/17 13:10
beego的底层其实也是封装了官方库http。

http.Request中有如下成员字段:
// RemoteAddr allows HTTP servers and other software to record
// the network address that sent the request, usually for
// logging. This field is not filled in by ReadRequest and
// has no defined format. The HTTP server in this package
// sets RemoteAddr to an "IP:port" address before invoking a
// handler.
// This field is ignored by the HTTP client.
RemoteAddr string

所以在beego的控制器中可以如此获取ip:
func (u *UserController) Suggest() {
req := u.Ctx.Request
addr := req.RemoteAddr // "IP:port" "192.168.1.150:8889"
beego.BeeLogger.Debug("addr:%s", addrr)
}