Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
csv write in go.
package main
import(
//"fmt"
"encoding/csv"
"os"
)
func main(){
file, err := os.OpenFile("mycsv.csv", os.O_CREATE|os.O_WRONLY, 0777)
defer file.Close()
if err != nil {
os.Exit(1)
}
x := []string{"1", "2", "3"}
y := []string{"a", "b", "c"}
csvWriter := csv.NewWriter(file) // file here satisfies the Writer Interface
csvWriter.Write(x)
csvWriter.Write(y)
csvWriter.Flush()
xy := [][]string{x, y}
csvWriter.WriteAll(xy)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment