dongmen5867 2015-12-28 08:14
浏览 50
已采纳

在Golang中新增JSON编码器类型的数组或映射

I am pretty new in Golang and encountered the following problem.

// XXX a bit inefficient. could open r files and run over list once
for r := 0; r < nreduce; r++ {
    file, err = os.Create(ReduceName(fileName, JobNumber, r))
    if err != nil {
        log.Fatal("DoMap: create ", err)
    }
    enc := json.NewEncoder(file)
    for e := res.Front(); e != nil; e = e.Next() {
        kv := e.Value.(KeyValue)
        if ihash(kv.Key)%uint32(nreduce) == uint32(r) {
            err := enc.Encode(&kv)
            if err != nil {
                log.Fatal("DoMap: marshall ", err)
            }
        }
    }
    file.Close()
}

Basically this code snippet creates a file in each for-loop iteration and then open a file to encode the stuff which belongs to this file, according to a (key, value) pair. However, this code is inefficient since it scan this file for too many times. The more efficient way to do so is to open r files and run over this list file once. So I would like to write like this (but I don't know how to do so):

enc_map := make(map[int]*Encode)
for r := 0; r < nreduce; r++ {
file. err = os.Create(ReduceName(fileName, JobNumber, r))
if err != nil {
    log.Fatal("DoMap: create ", err)
}
enc := json.NewEncoder(file)
enc_map[r] = enc
for e := res.Front(); e != nil; e = e.Next() {
    kv := e.Value.(KeyValue)
    r := ihash(kv.Key)&uint32(nreduce) 
    err := enc_map[r].Encode(&kv)
    if err != nil {
        log.Fatal("DoMap: marshall ", err)
    }
}

This code snippets first creates a map that saves the json.Encoder objects and then iterate over this file once. I looked up the go documentation and it says the type name of json.Encoder is *Encode. But the line

enc_map := make(map[int]*Encode)

is wrong and the compiler gives me the following error:

../mapreduce/mapreduce.go:228: undefined: Encode
../mapreduce/mapreduce.go:230: file.err undefined (type *os.File has no field or method err)
../mapreduce/mapreduce.go:230: multiple-value os.Create() in single-value context

So what's the right way to do things?

  • 写回答

1条回答 默认 最新

  • douya5331 2015-12-28 09:13
    关注

    json.NewEncoder(file) returns an object of type *Encoder. Therefore, try editing the line:

    enc_map := make(map[int]*Encode)
    

    to:

    enc_map := make(map[int]* json.Encoder)
    

    to eliminate the error:

    ../mapreduce/mapreduce.go:228: undefined: Encode
    

    To get rid of the other 2 errors, i.e.,

    ../mapreduce/mapreduce.go:230: file.err undefined (type *os.File has no field or method err)
    ../mapreduce/mapreduce.go:230: multiple-value os.Create() in single-value context
    

    just edit file .err in line:

    file. err = os.Create(ReduceName(fileName, JobNumber, r))
    

    to file, err in your code.

    I hope this solves the issue you're facing.

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作