package main
import (
"encoding/csv"
"fmt"
"io"
"os"
"time"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
"github.com/gocarina/gocsv"
)
const layout = "20060102150405"
type User struct {
Name string `csv:"名前"`
Mail string `csv:"メール"`
Age string `csv:"age"`
}
func main() {
var users []*User
users = append(users, &User{
Name: "ほげ",
Mail: "hoge@example.com",
Age: "21",
})
gocsv.SetCSVWriter(func(out io.Writer) *gocsv.SafeCSVWriter {
writer := csv.NewWriter(transform.NewWriter(out, japanese.ShiftJIS.NewEncoder()))
writer.UseCRLF = true
return gocsv.NewSafeCSVWriter(writer)
})
now := time.Now()
file, err := os.OpenFile(now.Format(layout)+".csv", os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
fmt.Println(err)
}
defer file.Close()
gocsv.MarshalFile(&users, file)
}