gpt4 book ai didi

go - panic : runtime error: invalid memory address or nil pointer dereference || r. 人体模型

转载 作者:数据小太阳 更新时间:2023-10-29 03:18:47 25 4
gpt4 key购买 nike

我的 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.ReadCloser 的包文档:

For client requests, a nil body means the request has no body, such as a GET request. For server requests, the Request Body is always non-nil.

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

data 是一个nil 指针,&data 是一个指向nil 指针的指针。

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

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

如果数据是一个接口(interface),首先为其分配一个真正的值,然后再次传递而不引用Decode:errDecode := json.NewDecoder(r.Body).Decode(数据)

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

关于go - panic : runtime error: invalid memory address or nil pointer dereference || r. 人体模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57130942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com