Golang write CSV records
The csv package have a
NewWriter()
function which returns a Writer
object which is used for writing CSV data. A csv.Writer
writes csv records which are terminated by a newline and uses a comma as the field delimiter. The following source code snippet shows how to write data to a CSV file.
Example
package main
import (
"encoding/csv"
"log"
"os"
)
func main() {
rows := [][]string{
{"Name", "City", "Language"},
{"Pinky", "London", "Python"},
{"Nicky", "Paris", "Golang"},
{"Micky", "Tokyo", "Php"},
}
csvfile, err := os.Create("test.csv")
if err != nil {
log.Fatalf("failed creating file: %s", err)
}
csvwriter := csv.NewWriter(csvfile)
for _, row := range rows {
_ = csvwriter.Write(row)
}
csvwriter.Flush()
csvfile.Close()
}
Most Helpful This Week
How to print string with double quote in Go?
Go program to find Name Server (NS) record of a domain
How to declare Interface Type in Go Programming Language
Different ways for Integer to String Conversions
Multiple-value <function> in single-value context error in Golang
Invalid operation: <variable> (type <type>) does not support indexing error in Golang
GO Program to Check Whether a Number is Palindrome or Not
Assign Default Value for Struct Field
Unveiling the Potential of Quantum Computing in AI: A Game Changer for Industries
Top Programming Languages Behind Blockchain App Development