Golang中如何从req.Response.StatusCode读取StatusCode
Golang中如何从req.Response.StatusCode读取StatusCode 我遇到了一个错误:
无效的内存地址或空指针解引用
我的代码如下所示:
func (ctr *Controller) Login(c *common.CPContext, req *http.Request, res http.ResponseWriter) (int, interface{}) {
defer func() {
var code = req.Response.StatusCode; // 错误在这里!!
log.Error(code)
if code < 400 {
user.LoginAttempts = 0
} else {
user.LoginAttempts = user.LoginAttempts + 1
}
_, err := user.Update(ctx, db, boil.Infer())
if err != nil {
log.Warningf("could not save/update user model: %v", err)
}
}()
// ...
}
在这里我应该如何处理指针的情况?我只是想获取 http.Response,特别是这里的 http.Response.StatusCode…
更多关于Golang中如何从req.Response.StatusCode读取StatusCode的实战教程也可以访问 https://www.itying.com/category-94-b0.html
4 回复
嗨,这个可能也有帮助:https://github.com/Erilbeth/gocheck
更多关于Golang中如何从req.Response.StatusCode读取StatusCode的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
我的猜测是,当你进入延迟函数时,req 已经被清理了。尝试将 req 传递给函数,看看是否能解决你的问题 😊
当你的延迟函数被调用时,所有东西都“消失”了。
看看 https://github.com/thoas/stats/blob/master/stats.go,它实现了你想要的功能。他们在记录器内部使用了 http.Hijacker。


