这次因为要给产品统计一些游戏数据,需要直接倒出他们直接可用的CSV,通过百度学习了csv文件的创建和写入,仅以此篇做记录。
//生成csv文件
func CreateCSV(txtname string,title [][]string) {
f , err := os.Create(txtname)
if err != nil{
panic(err)
}
defer f.Close()
f.WriteString("\xEF\xBB\xBF")
w:=csv.NewWriter(f)
w.WriteAll(title)
w.Flush()
}
func SetGiftLog(giftcount [10]int64,giftprice [10]int64) {
var giftlog []string
if len(action.giftLog) ==0{
title:=[]string{"giftid","giftcount","giftprice"}
action.giftLog=append(action.giftLog,title)
}
for i:=0;i<10;i++{
giftlog=make([]string,0)
strgiftid:=fmt.Sprintf("%d",i)
giftlog=append(giftlog,strgiftid)
strgiftcount:=fmt.Sprintf("%d",giftcount[i])
giftlog=append(giftlog,strgiftcount)
strgiftprice:=fmt.Sprintf("%d",giftprice[i])
giftlog=append(giftlog,strgiftprice)
action.giftLog=append(action.giftLog,giftlog)
}
}
函数CreateCSV是csv文件的创建和写入,SetGiftLog是关于写入内容的编辑
网友评论