Golang encoding/csv.Writer.Write() function example
package encoding/csv
Writer writes a single CSV record to w(given input type) along with any necessary quoting. A record is a slice of strings with each string being one field.
Golang encoding/csv.Writer.Write() function usage example
package main
import (
"encoding/csv"
"fmt"
"os"
)
func main() {
csvfile, err := os.Create("output.csv")
if err != nil {
fmt.Println("Error:", err)
return
}
defer csvfile.Close()
records := [][]string{{"item1", "value1"}, {"item2", "value2"}, {"item3", "value3"}}
writer := csv.NewWriter(csvfile)
for _, record := range records {
err := writer.Write(record) // <-- write 1 a single record
if err != nil {
fmt.Println("Error:", err)
return
}
}
writer.Flush()
}
Reference :
Advertisement
Something interesting
Tutorials
+11.4k Golang : "https://" not allowed in import path
+14.9k Golang : Get IP addresses of a domain name
+6.9k Golang : Get all countries phone codes
+10.5k Golang : Verify Linux user password again before executing a program example
+26.6k Get file path of temporary file in Go
+16.1k Golang : When to use init() function?
+4k HTTP common errors and their meaning explained
+11.5k Golang : Get terminal width and height example
+7.1k Prevent Write failed: Broken pipe problem during ssh session with screen command
+9.4k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+7.4k Golang : Generate Datamatrix barcode
+21.2k Golang : Set and Get HTTP request headers example