Efficient read and write CSV in Go

前端 未结 3 637
日久生厌
日久生厌 2020-12-28 09:27

The Go code below reads in a 10,000 record CSV (of timestamp times and float values), runs some operations on the data, and then writes the origina

3条回答
  •  猫巷女王i
    2020-12-28 10:23

    You're loading the file in memory first then processing it, that can be slow with a big file.

    You need to loop and call .Read and process one line at a time.

    func processCSV(rc io.Reader) (ch chan []string) {
        ch = make(chan []string, 10)
        go func() {
            r := csv.NewReader(rc)
            if _, err := r.Read(); err != nil { //read header
                log.Fatal(err)
            }
            defer close(ch)
            for {
                rec, err := r.Read()
                if err != nil {
                    if err == io.EOF {
                        break
                    }
                    log.Fatal(err)
    
                }
                ch <- rec
            }
        }()
        return
    }
    

    playground

    //note it's roughly based on DaveC's comment.

提交回复
热议问题