第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

恐慌:運行時錯誤:無效的內(nèi)存地址或零指針取消引用 || r.身體模型

恐慌:運行時錯誤:無效的內(nèi)存地址或零指針取消引用 || r.身體模型

Go
慕運維8079593 2023-07-04 19:05:07
我的 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}
查看完整描述

1 回答

?
慕森卡

TA貢獻1806條經(jīng)驗 獲得超8個贊

根據(jù) Body io.ReadCloser 的包文檔:

對于客戶端請求,nil body 意味著該請求沒有 body,例如 GET 請求。對于服務器請求,請求正文始終不為零。

你說,它是一個服務器,所以,它總是非零,因此錯誤出現(xiàn)在Decode你將指針傳遞給零指針的調(diào)用中。

data是一個nil指針,&data是一個指向nil指針的指針。

如果數(shù)據(jù)是非接口類型,除非models包明確禁止,否則解決方案new(EditProfile)

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

如果 data 是一個接口,則首先為其分配一個實際值,然后再次傳遞而不引用DecodeerrDecode := json.NewDecoder(r.Body).Decode(data)

在后一種情況下,還要仔細檢查您是否聲明了正確的類型*models.EditProfile。


查看完整回答
反對 回復 2023-07-04
  • 1 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號