Sample program to create csv and write data
The following source code snippet write 3 lines in CSV and create a new csv using
OpenFile
function
Example
package main
import (
"encoding/csv"
"os"
)
func main() {
file, err := os.OpenFile("test.csv", os.O_CREATE|os.O_WRONLY, 0777)
defer file.Close()
if err != nil {
os.Exit(1)
}
x := []string{"Country", "City", "Population"}
y := []string{"Japan", "Tokyo", "923456"}
z := []string{"Australia", "Sydney", "789650"}
csvWriter := csv.NewWriter(file)
strWrite := [][]string{x, y, z}
csvWriter.WriteAll(strWrite)
csvWriter.Flush()
}
Most Helpful This Week
How to get first and last element of slice in Golang?
Replace first occurrence of string using Regexp
Add N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time
Various examples of Carbon date-time package in Golang
Example: How to use TeeReader from IO Package in Golang?
How to read input from console line?