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 split a string on white-space?
How to wait for Goroutines to Finish Execution?
How to fix race condition using Atomic Functions in Golang?
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]
Select single argument from all arguments of variadic function
How to print string with double quote in Go?
How to convert Boolean Type to String in Go?
Find capacity of Channel, Pointer and Slice
Example of Pointers with Struct
How to import and alias package names?