{"id":208023,"date":"2025-07-08T09:34:09","date_gmt":"2025-07-08T01:34:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208023\/"},"modified":"2025-07-08T09:34:09","modified_gmt":"2025-07-08T01:34:09","slug":"imagekit-go-%e8%af%ad%e8%a8%80-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e4%b8%8a%e4%b8%8b%e6%96%87","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208023\/","title":{"rendered":"Imagekit &#8211; Go \u8bed\u8a00 |\u5982\u4f55\u4f7f\u7528\u4e0a\u4e0b\u6587"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Imagekit &#8211; Go \u8bed\u8a00 |\u5982\u4f55\u4f7f\u7528\u4e0a\u4e0b\u6587<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 22:57:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aImagekit &#8211; Go \u8bed\u8a00 |\u5982\u4f55\u4f7f\u7528\u4e0a\u4e0b\u6587\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u901a\u8fc7 imagekit sdk \u5c06\u56fe\u50cf\u4ece golang \u540e\u7aef\u4e0a\u4f20\u5230 imagekit\u3002\u6211\u9762\u4e34\u7740\u7406\u89e3 context api \u7528\u9014\u7684\u95ee\u9898\u3002<\/p>\n<p>\u76ee\u524d\uff0c\u6211\u5e76\u6ca1\u6709\u771f\u6b63\u5728\u540e\u7aef\u4f7f\u7528\u4e0a\u4e0b\u6587\uff0c\u800c\u662f\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f20\u9012\u6240\u6709\u4f9d\u8d56\u9879\u3002<\/p>\n<p>\u6211\u9047\u5230\u4e86\u5f02\u5e38\uff1a<\/p>\n<pre>2023\/01\/04 09:53:56 http: panic serving 127.0.0.1:50780: runtime error: invalid memory address or nil pointer dereference\ngoroutine 20 [running]:\nnet\/http.(*conn).serve.func1()\n        \/usr\/local\/cellar\/go\/1.19.3\/libexec\/src\/net\/http\/server.go:1850 +0xbf\npanic({0x1005836c0, 0x10092b820})\n        \/usr\/local\/cellar\/go\/1.19.3\/libexec\/src\/runtime\/panic.go:890 +0x262\ngithub.com\/bolzfieber\/bolzfieber-backend\/internal\/controllers.pitchescontroller.createandupdatepitch({0xc0001f7530?, 0x0?}, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)\n        \/users\/pbahr\/dev\/bolzfieber\/be\/bolzfieber-backend\/internal\/controllers\/pitches.go:91 +0x5ae\nnet\/http.handlerfunc.servehttp(0x100580980?, {0x1006a94e8?, 0xc0000ca000?}, 0xc0000be048?)\n        \/usr\/local\/cellar\/go\/1.19.3\/libexec\/src\/net\/http\/server.go:2109 +0x2f\ngithub.com\/go-chi\/chi\/v5.(*mux).routehttp(0xc00007c600, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)\n        \/users\/pbahr\/go\/pkg\/mod\/github.com\/go-chi\/chi\/[email&nbsp;protected]\/mux.go:442 +0x216\nnet\/http.handlerfunc.servehttp(0xc0000b00c0?, {0x1006a94e8?, 0xc0000ca000?}, 0xc00011b918?)\n        \/usr\/local\/cellar\/go\/1.19.3\/libexec\/src\/net\/http\/server.go:2109 +0x2f\ngithub.com\/go-chi\/chi\/v5.(*mux).servehttp(0xc00007c600, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)\n        \/users\/pbahr\/go\/pkg\/mod\/github.com\/go-chi\/chi\/[email&nbsp;protected]\/mux.go:71 +0x355\ngithub.com\/go-chi\/chi\/v5.(*mux).mount.func1({0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)\n        \/users\/pbahr\/go\/pkg\/mod\/github.com\/go-chi\/chi\/[email&nbsp;protected]\/mux.go:314 +0x19c\nnet\/http.handlerfunc.servehttp(0x100580980?, {0x1006a94e8?, 0xc0000ca000?}, 0xc0000aa005?)\n        \/usr\/local\/cellar\/go\/1.19.3\/libexec\/src\/net\/http\/server.go:2109 +0x2f\ngithub.com\/go-chi\/chi\/v5.(*mux).routehttp(0xc00007c420, {0x1006a94e8, 0xc0000ca000}, 0xc0000d4000)\n        \/users\/pbahr\/go\/pkg\/mod\/github.com\/go-chi\/chi\/[email&nbsp;protected]\/mux.go:442 +0x216\nnet\/http.handlerfunc.servehttp(0x1006a9bf8?, {0x1006a94e8?, 0xc0000ca000?}, 0x10092b640?)\n        \/usr\/local\/cellar\/go\/1.19.3\/libexec\/src\/net\/http\/server.go:2109 +0x2f\ngithub.com\/go-chi\/chi\/v5.(*mux).servehttp(0xc00007c420, {0x1006a94e8, 0xc0000ca000}, 0xc000332000)\n        \/users\/pbahr\/go\/pkg\/mod\/github.com\/go-chi\/chi\/[email&nbsp;protected]\/mux.go:88 +0x310\nnet\/http.serverhandler.servehttp({0x1006a84d0?}, {0x1006a94e8, 0xc0000ca000}, 0xc000332000)\n        \/usr\/local\/cellar\/go\/1.19.3\/libexec\/src\/net\/http\/server.go:2947 +0x30c\nnet\/http.(*conn).serve(0xc00032c000, {0x1006a9ca0, 0xc000318270})\n        \/usr\/local\/cellar\/go\/1.19.3\/libexec\/src\/net\/http\/server.go:1991 +0x607\ncreated by net\/http.(*server).serve\n        \/usr\/local\/cellar\/go\/1.19.3\/libexec\/src\/net\/http\/server.go:3102 +0x4db<\/pre>\n<p>\u800c\u7b2c91\u884c\u662fimagekit\u7684\u4e0a\u4f20\u884c\u3002\u6240\u4ee5\u6211\u8ba4\u4e3a\u7a7a\u6307\u9488\u662f\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>\u6211\u5f53\u524d\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>func (c PitchesController) CreateAndUpdatePitch(w http.ResponseWriter, r *http.Request) {\n    var pitchUpload models.PitchUpload\n    err := json.NewDecoder(r.Body).Decode(&amp;pitchUpload)\n\n    if err != nil {\n        fmt.Printf(\"Error: %v\\n\", err)\n        utils.RespondWithError(w, http.StatusBadRequest, \"Bad request\")\n        return\n    }\n\n    var pitch = models.Pitch{\n        Name:            pitchUpload.Name,\n        Address:         pitchUpload.Address,\n        Author:          pitchUpload.Author,\n        PitchType:       pitchUpload.PitchType,\n        PitchSize:       pitchUpload.PitchSize,\n        GoalSize:        pitchUpload.GoalSize,\n        GoalType:        pitchUpload.GoalType,\n        LocationScopeId: pitchUpload.LocationScopeId,\n        Coordinates:     pitchUpload.Coordinates,\n        Rating:          pitchUpload.Rating,\n    }\n\n    for i, image := range pitchUpload.Images {\n        fmt.Printf(\"pitch: %v\\n\", pitchUpload.Name)\n        response, uploadErr := c.Ik.Uploader.Upload(r.Context(), image, uploader.UploadParam{\n            FileName:          pitchUpload.Name + \"_\" + string(rune(i)),\n            UseUniqueFileName: newTrue(),\n            Tags:              \"pitch\",\n            Folder:            \"\/pitches\/\" + pitchUpload.Name,\n        })\n\n        if uploadErr != nil {\n            fmt.Printf(\"Error: %v\\n\", uploadErr)\n            utils.RespondWithError(w, http.StatusBadRequest, \"Bad request\")\n            return\n        }\n\n        pitch.Images = append(pitch.Images, models.PitchImage{FileUrl: response.Data.Url})\n    }\n\n    c.Db.Save(&amp;pitch)\n\n    utils.RespondWithJSON(w, http.StatusCreated, \"Created successfully\")\n}\n<\/pre>\n<p>\u4ee5\u4e0b\u6b65\u9aa4\u9002\u7528\uff1a<\/p>\n<ol>\n<li>\u63a5\u6536\u5e16\u5b50\u6b63\u6587\u5e76\u89e3\u7801\u4e3a\u6709\u6548\u7684 go \u7ed3\u6784<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ed3\u6784\uff0c\u7a0d\u540e\u5c06\u5176\u4fdd\u5b58\u5230\u6570\u636e\u5e93<\/li>\n<li>\u5faa\u73af\u7ed3\u6784\u4f53\u7684\u56fe\u50cf\uff08base64 \u5b57\u7b26\u4e32\uff09<\/li>\n<li>\u5c06\u6bcf\u5f20\u56fe\u7247\u4e0a\u4f20\u5230 imagekit<\/li>\n<li>\u5c06 imagekit \u54cd\u5e94\u9644\u52a0\u5230\u5e94\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u7684\u7ed3\u6784<\/li>\n<li>\u4fdd\u5b58\u5230\u6570\u636e\u5e93<\/li>\n<\/ol>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5728\u8fd9\u4e00\u884c\u5f97\u5230\u4e00\u4e2a nil \u6307\u9488\u53ef\u80fd\u610f\u5473\u7740 c.Ik\u3002\u6216 c.Ik.Uploader\u3002\u6216\u6cb3\u3002\u6216\u4e0a\u4f20\u8005\u3002\u662f\u96f6\u3002\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u6d88\u9664\u4e00\u4e9b\u5acc\u7591\u4eba\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aImagekit &#8211; Go \u8bed\u8a00 |\u5982\u4f55\u4f7f\u7528\u4e0a\u4e0b\u6587\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208023","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208023"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208023\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}