"Si un ouvrier veut bien faire son travail, il doit d'abord affûter ses outils." - Confucius, "Les Entretiens de Confucius. Lu Linggong"
Page de garde > La programmation > How to Efficiently Read and Write CSV Files in Go?

How to Efficiently Read and Write CSV Files in Go?

Publié le 2024-11-08
Parcourir:612

How to Efficiently Read and Write CSV Files in Go?

Efficient Read and Write of CSV Files in Go

One common task in data processing is reading and writing CSV files in a performant manner. The code snippet provided in the question demonstrates a slow method for reading a CSV file, processing the data, and writing it back out. A potential inefficiency lies in the approach of loading the entire file into memory before processing.

To optimize the code, it is recommended to read the file incrementally by calling .Read() and process one line at a time. This prevents the entire file from being loaded into memory, which can improve performance significantly, especially for large files.

Here is an alternative approach:

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
}

This approach uses a channel to pass records from the reader goroutine to the main goroutine for processing, allowing for a more efficient incremental processing approach.

By adopting this technique, which involves reading and processing data incrementally, you can significantly improve the performance of your CSV reading and writing code.

Dernier tutoriel Plus>

Clause de non-responsabilité: Toutes les ressources fournies proviennent en partie d'Internet. En cas de violation de vos droits d'auteur ou d'autres droits et intérêts, veuillez expliquer les raisons détaillées et fournir une preuve du droit d'auteur ou des droits et intérêts, puis l'envoyer à l'adresse e-mail : [email protected]. Nous nous en occuperons pour vous dans les plus brefs délais.

Copyright© 2022 湘ICP备2022001581号-3