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
+8.3k Golang : Oanda bot with Telegram and RSI example
+6.8k Android Studio : Hello World example
+14.6k Golang : GUI with Qt and OpenCV to capture image from camera
+10.5k Generate Random number with math/rand in Go
+15.2k Golang : Get timezone offset from date or timestamp
+25.9k Golang : How to read integer value from standard input ?
+6.8k Golang : Join lines with certain suffix symbol example
+8.8k Golang : Take screen shot of browser with JQuery example
+9k Golang : Get SPF and DMARC from email headers to fight spam
+4.9k Python : Find out the variable type and determine the type with simple test
+13.6k Golang : reCAPTCHA example
+14.3k Golang : Simple word wrap or line breaking example