这是我参与「第五届青训营 」伴学笔记创作活动的第7天
错误信息
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x40 pc=0x1027b7c24]
错误定位
package service
func (usi *UserServiceImpl) GetUserLoginInfoById(id int64) (User, error) {
...
// 计算关注数
followCnt, _ := usi.GetFollowingCnt(id)
// 计算粉丝数
followerCnt, _ := usi.GetFollowerCnt(id)
...
}
错误分析
usi是nil, 直接调用指针就会报错。不能直接在函数里调用。要初始化服务对象。
Golang错误解析“runtime error: invalid memory address or nil pointer dereference”_chrispink_yang的
userService := GetUserServiceInstance()
// 计算关注数
followCnt, _ := userService.GetFollowingCnt(id)
// 计算粉丝数
followerCnt, _ := userService.GetFollowerCnt(id)