golang写入csv

 1 package main
 2 
 3 import (
 4     "encoding/csv"
 5     "fmt"
 6     "os"
 7 )
 8 
 9 func main() {
10     file, err := os.OpenFile("111.csv", os.O_CREATE|os.O_RDWR, 0644)
11     if err != nil {
12         fmt.Println("open file is failed, err: ", err)
13     }
14     defer file.Close()
15     // 写入UTF-8 BOM,防止中文乱码
16     file.WriteString("\xEF\xBB\xBF")
17     w := csv.NewWriter(file)
18     w.Write([]string{"开发者名称", "开发者邮箱", "应用名称"})
19     // 写文件需要flush,不然缓存满了,后面的就写不进去了,只会写一部分
20     w.Flush()
21 }

 

转载于:https://www.cnblogs.com/chaselogs/p/9454953.html

你可能感兴趣的:(golang写入csv)