golang生成csv

2016-09-28 16:58 来源:www.chinab4c.com 作者:admins

 package main

 
import (
"encoding/csv"
"log"
"os"
"bufio"
"fmt"
)
 
func main() {
records := [][]string{
{"first_name", "last_name", "username"},
{"Rob", "Pike", ""},
{"Ken", "", "ken"},
{"Robert", "测试", "gri"},
}
w3, err := os.Create("a.csv")
if err != nil {
}
w := csv.NewWriter(bufio.NewWriter(w3))
 
for _, record := range records {
if err := w.Write(record); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
 
// Write any buffered data to the underlying writer (standard output).
w.Flush()
 
if err := w.Error(); err != nil {
log.Fatal(err)
}
}
 
func xx(){
os.Exit(1)
fmt.Println(2)
}