duanfu3634 2019-08-28 00:24
浏览 64
已采纳

为什么地图值不存在?

I am using a map to store random string keys to *os.File objects. Users will be uploading a file and I want to hold a reference to the file in a global map so I can delete it later.

I have an http handler to process the upload and at the end, I map a random key from the OS uuidgen to "logBundleFile" which is type *os.File.

var db = map[string]*os.File{}

func uploadHandler(w http.ResponseWriter, r *http.Request) {
    r.ParseMultipartForm(5 << 30)
    file, handler, err := r.FormFile("file")
    if err != nil {
        log.Fatalf("Error retrieving the file: %v", err)
        return
    }
    defer file.Close()

    logBundleFile, err := ioutil.TempFile("", handler.Filename)
    if err != nil {
        log.Fatal(err)
    }
    defer logBundleFile.Close()

    fileBytes, err := ioutil.ReadAll(file)
    if err != nil {
        log.Fatalf("Error reading file: %v", err)
    }
    logBundleFile.Write(fileBytes)

    id, err := exec.Command("uuidgen").Output()
    idStr := string(id[:])
    //id := "1"
    if err != nil {
        log.Fatal(err)
    }
    db[idStr] = logBundleFile
    log.Printf("ID: %v Type: %T
", idStr, idStr)
    log.Printf("val: %v Type: %T

", db[idStr], db[idStr])
    http.Redirect(w, r, fmt.Sprintf("/%s", idStr), http.StatusMovedPermanently)
}

Once that is done, you get redirected to this sessionHandler. It will check if the ID in the body is valid, i.e, mapped to a *os.File. The "ok" bool is always returning false.

func sessionHandler(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    id := vars["id"]
    log.Printf("ID: %v Type: %T
", id, id)
    log.Printf("val: %v Type: %T
", db[id], db[id])
    if val, ok := db[id]; ok {
        w.Write([]byte(fmt.Sprintf("Session %s %v", id, val)))
    } else {
        http.Redirect(w, r, "/", http.StatusMovedPermanently)
    }
}

Here is an output from the prints. In the uploadHandler, we can see that we have a string key mapped to a non-nil *os.File.

But in the session handler, the same string key maps to a nil *os.File. I don't know what is going on.

2019/08/27 19:49:49 ID: BA06C157-451E-48B5-85F9-8069D9A4EFCE
 Type: string
2019/08/27 19:49:49 val: &{0xc000160120} Type: *os.File

2019/08/27 19:49:49 ID: BA06C157-451E-48B5-85F9-8069D9A4EFCE Type: string
2019/08/27 19:49:49 val: <nil> Type: *os.File
  • 写回答

1条回答 默认 最新

  • dongsheng1238 2019-08-28 03:06
    关注

    It's because in the uploadHandler, the id variable contains newline. If we take a look closely on the log we can see it. somehow Type: string text is printed in the 2nd line.

    2019/08/27 19:49:49 ID: BA06C157-451E-48B5-85F9-8069D9A4EFCE // <-- newline
     Type: string
    2019/08/27 19:49:49 ID: BA06C157-451E-48B5-85F9-8069D9A4EFCE Type: string
    

    Putting trim operation on the idStr should solve the problem.

    idStr := strings.TrimSpace(string(id[:]))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助