首页 >后端开发 >Golang >Imagekit - Go 语言 |如何使用上下文

Imagekit - Go 语言 |如何使用上下文

王林
王林转载
2024-02-15 18:45:081071浏览

Imagekit - Go 语言 |如何使用上下文

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")
}

以下步骤适用:

  1. 接收帖子正文并解码为有效的 go 结构
  2. 创建一个新的结构,稍后将其保存到数据库
  3. 循环结构体的图像(base64 字符串)
  4. 将每张图片上传到 imagekit
  5. 将 imagekit 响应附加到应保存到数据库的结构
  6. 保存到数据库

解决方法

在这一行得到一个 nil 指针可能意味着 c.Ik。或 c.Ik.Uploader。或河。或上传者。是零。我们可以轻松消除一些嫌疑人。

以上是Imagekit - Go 语言 |如何使用上下文的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除