Answers for "how to create csv file in golang"

1

how to create csv file in golang

package main

import (
	"encoding/csv"
	"log"
	"os"
)

func main() {
	empData := [][]string{
		{"Name", "City"},
		{"Smith", "India"},
	}
    
	csvFile, err := os.Create("fileName.csv")

	if err != nil {
		log.Fatalf("failed creating file: %s", err)
	}

	csvwriter := csv.NewWriter(csvFile)

	for _, empRow := range empData {
		_ = csvwriter.Write(empRow)
	}
	csvwriter.Flush()
	csvFile.Close()
}
Posted by: Guest on April-07-2021

Code answers related to "how to create csv file in golang"

Browse Popular Code Answers by Language