我的 Go 編程新手遇到問題,例如:無效的內(nèi)存地址或 nil 指針取消引用有時我可以解決問題,但這讓我感到困惑。這是處理程序級別的代碼,我嘗試實現(xiàn) ###p.repo.UpdateProfile() 和來自 r.body 解碼的數(shù)據(jù)//UpdateProfile handlerfunc (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}
恐慌:運行時錯誤:無效的內(nèi)存地址或零指針取消引用 || r.身體模型
慕運維8079593
2023-07-04 19:05:07