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
+8.3k Golang : How to get ECDSA curve and parameters data?
+4.2k Unix/Linux : secure copying between servers with SCP command examples
+5.4k Golang : Test input string for unicode example
+9.1k Golang : Wait and sync.WaitGroup example
+6k Golang : Get Alexa ranking data example
+25.5k Golang : dial tcp: too many colons in address
+10.4k Golang : Surveillance with web camera and OpenCV
+25.1k Golang : Encrypt and decrypt data with AES crypto
+6.8k Golang : Error reading timestamp with GORM or SQL driver
+44.5k Golang : Encode image to base64 example
+4.8k Linux : Disable and enable IPv4 forwarding
+28.8k Golang : Remove characters from string example