日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

php小編蘋果為大家帶來了關于Go語言中如何使用上下文的介紹。在Go語言開發中,上下文(Context)是一個非常重要的概念,它可以在多個goroutine之間傳遞請求的信息。本文將詳細介紹上下文的作用、使用場景以及如何在Go語言中使用上下文。無論你是初學者還是有一定經驗的開發者,通過閱讀本文,你都能夠更好地理解和應用上下文在Go語言開發中的重要性。

問題內容

我正在嘗試通過 imagekit sdk 將圖像從 golang 后端上傳到 imagekit。我面臨著理解 context api 用途的問題。

目前,我并沒有真正在后端使用上下文,而是在構造函數中傳遞所有依賴項。

我遇到了異常:

2023/01/04 09:53:56 http: panic serving 127.0.0.1:50780: runtime error: invalid memory address or nil pointer dereference
goroutine 20 [running]:
net/http.(*conn).serve.func1()
        /usr/local/cellar/go/1.19.3/libexec/src/net/http/server.go:1850 +0xbf
panic({0x1005836c0, 0x10092b820})
        /usr/local/cellar/go/1.19.3/libexec/src/runtime/panic.go:890 +0x262
github.com/bolzfieber/bolzfieber-backend/internal/controllers.pitchescontroller.createandupdatepitch({0xc0001f7530?, 0x0?}, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /users/pbahr/dev/bolzfieber/be/bolzfieber-backend/internal/controllers/pitches.go:91 +0x5ae
net/http.handlerfunc.servehttp(0x100580980?, {0x1006a94e8?, 0xc0000ca000?}, 0xc0000be048?)
        /usr/local/cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*mux).routehttp(0xc00007c600, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /users/pbahr/go/pkg/mod/github.com/go-chi/chi/[email protected]/mux.go:442 +0x216
net/http.handlerfunc.servehttp(0xc0000b00c0?, {0x1006a94e8?, 0xc0000ca000?}, 0xc00011b918?)
        /usr/local/cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*mux).servehttp(0xc00007c600, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /users/pbahr/go/pkg/mod/github.com/go-chi/chi/[email protected]/mux.go:71 +0x355
github.com/go-chi/chi/v5.(*mux).mount.func1({0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /users/pbahr/go/pkg/mod/github.com/go-chi/chi/[email protected]/mux.go:314 +0x19c
net/http.handlerfunc.servehttp(0x100580980?, {0x1006a94e8?, 0xc0000ca000?}, 0xc0000aa005?)
        /usr/local/cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*mux).routehttp(0xc00007c420, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)
        /users/pbahr/go/pkg/mod/github.com/go-chi/chi/[email protected]/mux.go:442 +0x216
net/http.handlerfunc.servehttp(0x1006a9bf8?, {0x1006a94e8?, 0xc0000ca000?}, 0x10092b640?)
        /usr/local/cellar/go/1.19.3/libexec/src/net/http/server.go:2109 +0x2f
github.com/go-chi/chi/v5.(*mux).servehttp(0xc00007c420, {0x1006a94e8, 0xc0000ca000}, 0xc000332000)
        /users/pbahr/go/pkg/mod/github.com/go-chi/chi/[email protected]/mux.go:88 +0x310
net/http.serverhandler.servehttp({0x1006a84d0?}, {0x1006a94e8, 0xc0000ca000}, 0xc000332000)
        /usr/local/cellar/go/1.19.3/libexec/src/net/http/server.go:2947 +0x30c
net/http.(*conn).serve(0xc00032c000, {0x1006a9ca0, 0xc000318270})
        /usr/local/cellar/go/1.19.3/libexec/src/net/http/server.go:1991 +0x607
created by net/http.(*server).serve
        /usr/local/cellar/go/1.19.3/libexec/src/net/http/server.go:3102 +0x4db

登錄后復制

而第91行是imagekit的上傳行。所以我認為空指針是上下文。

我當前的方法如下:

func (c PitchesController) CreateAndUpdatePitch(w http.ResponseWriter, r *http.Request) {
    var pitchUpload models.PitchUpload
    err := json.NewDecoder(r.Body).Decode(&pitchUpload)

    if err != nil {
        fmt.Printf("Error: %v\n", err)
        utils.RespondWithError(w, http.StatusBadRequest, "Bad request")
        return
    }

    var pitch = models.Pitch{
        Name:            pitchUpload.Name,
        Address:         pitchUpload.Address,
        Author:          pitchUpload.Author,
        PitchType:       pitchUpload.PitchType,
        PitchSize:       pitchUpload.PitchSize,
        GoalSize:        pitchUpload.GoalSize,
        GoalType:        pitchUpload.GoalType,
        LocationScopeId: pitchUpload.LocationScopeId,
        Coordinates:     pitchUpload.Coordinates,
        Rating:          pitchUpload.Rating,
    }

    for i, image := range pitchUpload.Images {
        fmt.Printf("pitch: %v\n", pitchUpload.Name)
        response, uploadErr := c.Ik.Uploader.Upload(r.Context(), image, uploader.UploadParam{
            FileName:          pitchUpload.Name + "_" + string(rune(i)),
            UseUniqueFileName: newTrue(),
            Tags:              "pitch",
            Folder:            "/pitches/" + pitchUpload.Name,
        })

        if uploadErr != nil {
            fmt.Printf("Error: %v\n", uploadErr)
            utils.RespondWithError(w, http.StatusBadRequest, "Bad request")
            return
        }

        pitch.Images = append(pitch.Images, models.PitchImage{FileUrl: response.Data.Url})
    }

    c.Db.Save(&pitch)

    utils.RespondWithJSON(w, http.StatusCreated, "Created successfully")
}

登錄后復制

以下步驟適用:

    接收帖子正文并解碼為有效的 go 結構
    創建一個新的結構,稍后將其保存到數據庫
    循環結構體的圖像(base64 字符串)
    將每張圖片上傳到 imagekit
    將 imagekit 響應附加到應保存到數據庫的結構
    保存到數據庫

解決方法

在這一行得到一個 nil 指針可能意味著 c.Ik。或 c.Ik.Uploader。或河。或上傳者。是零。我們可以輕松消除一些嫌疑人。

分享到:
標簽:Go語言
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定