Skip to content

Instantly share code, notes, and snippets.

@khoa-le
Last active August 29, 2015 14:21
Show Gist options
  • Save khoa-le/2527b51748f9afe521c3 to your computer and use it in GitHub Desktop.
Save khoa-le/2527b51748f9afe521c3 to your computer and use it in GitHub Desktop.
How to write CSV data to file
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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment