duanbiyi7319 2014-12-28 06:31 采纳率: 100%
浏览 95
已采纳

Golang:恐慌:运行时错误:无效的内存地址或nil指针取消引用

When uploading a file to my go app, I encounter a panic.

 panic: runtime error: invalid memory address or nil pointer dereference
 /Users/bob/Projects/go/src/github.com/zenazn/goji/web/middleware/recoverer.go:24 (0xbaf5b)
    func.006: debug.PrintStack()
 /usr/local/go/src/pkg/runtime/panic.c:248 (0x1043d)
    panic: runtime·newstackcall(d->fn, (byte*)d->args, d->siz);
 /usr/local/go/src/pkg/runtime/panic.c:552 (0x10eed)
    panicstring: runtime·panic(err);
 /usr/local/go/src/pkg/runtime/os_darwin.c:454 (0xfb8e)
    sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference");
 /usr/local/go/src/pkg/mime/multipart/multipart.go:223 (0xb6801)
    (*Reader).NextPart: if r.currentPart != nil {
 /Users/bob/Projects/go/src/github.com/app/controllers/company_sheet_controller.go:32 (0x2ee18)
    NewCompanySheet: part, err := mr.NextPart()
 /usr/local/go/src/pkg/net/http/server.go:1235 (0x44f00)
    HandlerFunc.ServeHTTP: f(w, r)
 /Users/bob/Projects/go/src/github.com/zenazn/goji/web/router.go:113 (0x6bc0a)

This method handles an upload from a multipart form, extracting the file contents and boundary data. The r.FormFile method on request is used to set file and header. And in order to pull the additional data from the post, I use r.MultipartReader. From the error description I see r is already declared as ParseMultipartForm when using r.FormFile. When executing the function with the different request methods individually, I receive no errors. r.FormFile and r.MultipartReader work fine isolated. Am I unable to mix the two request methods?

func Upload(r *http.Request) {
  file, header, err := r.FormFile("file")
  ErrorCheck(err)

  mr, err := r.MultipartReader()
  ErrorCheck(err)
  part, err := mr.NextPart()
  ErrorCheck(err)

  var b bytes.Buffer
  io.CopyN(&b, part, int64(1<<20))
  fmt.Println(b.String())
  defer file.Close()
}
  • 写回答

1条回答 默认 最新

  • doulai8405 2014-12-28 11:19
    关注

    You are calling FormFile() at the beginning of your function.

    This calls ParseMultipartForm() (see Request.FormFile) which populates the MultipartForm field of your http.Request.

    Now the documentation for MultipartReader() states that you should use MultipartReader() instead of ParseMultipartForm() if you want to process the data as stream.

    Looking at the source MultipartReader() returns an error if the MultipartForm field was already set.

    So to answer your question: No, you can't use both functions for the same request.

    Also your

    defer file.Close()
    

    should be right after you checked for an error from FormFile(), otherwise the file won't be closed before Garbage Collection, when your function panics.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。