Last active
August 29, 2015 14:21
-
-
Save khoa-le/2527b51748f9afe521c3 to your computer and use it in GitHub Desktop.
How to write CSV data to file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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