panic:运行时错误:内存地址无效或 nil 指针取消引用 ||R.body模型



我有问题新手 Go 编程,例如:无效的内存地址或 nil 指针取消引用

有时我可以解决问题,但这让我感到困惑。这是处理程序级别的代码,我尝试实现###p.repo.UpdateProfile((和来自 R.body 解码的数据

//UpdateProfile handler
func (p *Profile) UpdateProfile(w http.ResponseWriter, r *http.Request) {
    var (
        errForm   models.ErrorForm
        resp      models.Response
        respError models.ErrorResponse
        errField  models.ErrField
        data      *models.EditProfile
    )
    userid := r.Context().Value(viper.GetString("token.userid"))
        
        errDecode := json.NewDecoder(r.Body).Decode(&data)
    errPayload := p.repo.UpdateProfile(r.Context(), data, userid)
    if errPayload.Error() == "username_exist" {
        respError.Message = "username already taken"
        respError.Status = 422
        lib.ResJSON(w, respError.Status, respError)
        return
    }
    lib.Catch(errPayload)
    resp.Data = ""
    resp.Message = "Success"
    resp.Status = 200
    lib.ResJSON(w, resp.Status, resp)
}

和方法如下:


func (m *mysqlProfileRepo) UpdateProfile(ctx context.Context, p *models.EditProfile, userid interface{}) error {
    query := ""
    var checkexist int
    row1, err1 := m.Conn.QueryContext(ctx, query, p.Username)
    if err1 != nil {
        return err1
    }
    for row1.Next() {
        if errSc1 := row1.Scan(&checkexist); errSc1 != nil {
            return errors.New("error_scan_db")
        }
    }
    if checkexist != 0 {
        return errors.New("username_exist")
    }
    query1 := ""
    query2 := ""
    stmts := []*lib.PipelineStmt{
        lib.NewPipelineStmt(query1, p.Image, p.Location, p.Link, p.Bio, p.Birthday, userid),
        lib.NewPipelineStmt(query2, p.Username, p.Fullname, userid),
    }
    errTrx := lib.WithTransaction(m.Conn, func(tx lib.Transaction) error {
        _, errRunPipe := lib.RunPipeline(tx, stmts...)
        return errRunPipe
    })
    if errTrx != nil {
        return errTrx
    }
    return nil
}

MyCode在数据库中运行良好,数据已成功更新,但响应服务器是

panic: runtime error: invalid memory address or nil pointer dereference
2019/07/21 13:27:36 goroutine 8 [running]:
runtime/debug.Stack(0xc00028e100, 0x16b1a20, 0xc000094040)
    /usr/local/Cellar/go/1.12.6/libexec/src/runtime/debug/stack.go:24 +0x9d
github.com/go-chi/chi/middleware.Recoverer.func1.1(0xc00028e100, 0x16b6be0, 0xc000256280)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/recoverer.go:25 +0x9c
panic(0x155e7a0, 0x1a66a10)
    /usr/local/Cellar/go/1.12.6/libexec/src/runtime/panic.go:522 +0x1b5
audioo_api/handler.(*Profile).UpdateProfile(0xc000020030, 0x16b6be0, 0xc000256280, 0xc00028e200)
    /Users/rizaldinurmuhammad/go/src/audioo_api/handler/profile.go:73 +0x524
net/http.HandlerFunc.ServeHTTP(0xc000020050, 0x16b6be0, 0xc000256280, 0xc00028e200)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
audioo_api/lib/mid.Authenticate.func1.1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/audioo_api/lib/mid/auth.go:48 +0x71e
net/http.HandlerFunc.ServeHTTP(0xc00000e0a0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*ChainHandler).ServeHTTP(0xc000066140, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/chain.go:31 +0x52
github.com/go-chi/chi.(*Mux).routeHTTP(0xc0000a5f80, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:425 +0x27f
net/http.HandlerFunc.ServeHTTP(0xc000020040, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0000a5f80, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:70 +0x451
github.com/go-chi/chi.(*Mux).Mount.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:292 +0x127
net/http.HandlerFunc.ServeHTTP(0xc00000e0e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).routeHTTP(0xc0000a59e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:425 +0x27f
net/http.HandlerFunc.ServeHTTP(0xc0001db880, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0000a59e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:70 +0x451
github.com/go-chi/chi.(*Mux).Mount.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:292 +0x127
net/http.HandlerFunc.ServeHTTP(0xc00000e180, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).routeHTTP(0xc0000a5920, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:425 +0x27f
net/http.HandlerFunc.ServeHTTP(0xc000020090, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
audioo_api/lib/mid.Limit.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/audioo_api/lib/mid/limit.go:79 +0xe5
net/http.HandlerFunc.ServeHTTP(0xc00000e1a0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/cors.(*Cors).Handler.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/cors/cors.go:201 +0x1a4
net/http.HandlerFunc.ServeHTTP(0xc00000e1c0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.Recoverer.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/recoverer.go:35 +0x9f
net/http.HandlerFunc.ServeHTTP(0xc00000e1e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.RedirectSlashes.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/strip.go:53 +0x202
net/http.HandlerFunc.ServeHTTP(0xc00000e200, 0x16b6be0, 0xc000256280, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.(*Compressor).Handler.func1.1(0x1f69180, 0xc000256240, 0xc00028e100)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/compress.go:190 +0x254
net/http.HandlerFunc.ServeHTTP(0xc00000e2c0, 0x1f69180, 0xc000256240, 0xc00028e100)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.RequestLogger.func1.1(0x16b78a0, 0xc000226000, 0xc00028e000)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/logger.go:46 +0x291
net/http.HandlerFunc.ServeHTTP(0xc00009a210, 0x16b78a0, 0xc000226000, 0xc00028e000)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0000a5920, 0x16b78a0, 0xc000226000, 0xc000154000)
    /Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:82 +0x294
net/http.serverHandler.ServeHTTP(0xc00009c410, 0x16b78a0, 0xc000226000, 0xc000154000)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:2774 +0xa8
net/http.(*conn).serve(0xc0002ce640, 0x16b8aa0, 0xc000067340)
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1878 +0x851
created by net/http.(*Server).Serve
    /usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:2884 +0x2f4

我试图更改上下文以实现数据库,但响应仍然相同。

我想知道与r.body相关的这个问题,但我不知道如何弄清楚

根据 Body io 的包文档。阅读关闭:

对于客户端请求,nil 正文表示请求没有正文,例如 GET 请求。对于服务器请求,请求正文始终为非 nil。

你说,它是一个服务器,所以,它总是非 nil,因此错误出现在Decode调用中,你将指针传递给 nil 指针。

datanil指针,&data是指向nil指针的指针。

数据是非接口类型的解决方案,除非models包明确禁止new(EditProfile)

    data = new(models.EditProfile)
    errDecode := json.NewDecoder(r.Body).Decode(data)

如果数据是一个接口,则首先为其分配一个实值,然后在不引用 Decode : 的情况下再次传递:errDecode := json.NewDecoder(r.Body).Decode(data)

在后一种情况下,还要仔细检查您是否声明了正确的类型 *models.EditProfile .

最新更新