Home > Backend Development > Golang > Imagekit - Go Language | How to use context

Imagekit - Go Language | How to use context

王林
Release: 2024-02-15 18:45:08
forward
1070 people have browsed it

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

php editor Apple brings you an introduction to how to use context in Go language. In Go language development, context is a very important concept, which can pass requested information between multiple goroutines. This article will introduce in detail the role of context, usage scenarios and how to use context in Go language. Whether you are a beginner or an experienced developer, by reading this article, you will be able to better understand and apply the importance of context in Go language development.

Question content

I am trying to upload an image from golang backend to imagekit via imagekit sdk. I'm facing problem understanding the purpose of context api.

Currently, I'm not really using context on the backend, but passing all dependencies in the constructor.

I encountered an exception:

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
Copy after login

And line 91 is the upload line of imagekit. So I think the null pointer is the context.

My current approach is as follows:

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")
}
Copy after login

The following steps apply:

  1. Receive the post body and decode it into a valid go structure
  2. Create a new structure and save it to the database later
  3. Image of loop structure (base64 string)
  4. Upload each image to imagekit
  5. Append the imagekit response to the structure that should be saved to the database
  6. Save to database

Workaround

Getting a nil pointer in this line probably means c.Ik. or c.Ik.Uploader. or river. or uploader. is zero. We can eliminate some suspects easily.

The above is the detailed content of Imagekit - Go Language | How to use context. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template