Golang encoding/csv.NewWriter() function example
package encoding/csv
NewWriter returns a new Writer that writes to w (1st parameter).
Golang encoding/csv.NewWriter() 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)
if err != nil {
fmt.Println("Error:", err)
return
}
}
writer.Flush()
}
Output : (content of output.csv)
item1,value1
item2,value2
item3,value3
Reference :
Advertisement
Something interesting
Tutorials
+5.2k Golang : Experimental Jawi programming language
+16.5k Golang : Execute terminal command to remote machine example
+15.9k Golang : Update database with GORM example
+6k Golang : Experimenting with the Rejang script
+9k Golang : How to use Gorilla webtoolkit context package properly
+6.5k Golang : Spell checking with ispell example
+17.7k Golang : [json: cannot unmarshal object into Go value of type]
+12.5k Golang : Forwarding a local port to a remote server example
+21.1k Golang : Sort and reverse sort a slice of strings
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+9.1k Golang : Intercept and compare HTTP response code example
+6.5k Golang : Calculate diameter, circumference, area, sphere surface and volume