csvãGoã®æ§é ä½ã«ãããã³ã°ãã
Goã§csvãæ±ãéã¯ãæ¨æºããã±ã¼ã¸ã®endording/csvã§å¯¾å¿ã§ãã¾ããããã¡ãã¡ã¹ã©ã¤ã¹ãæ±ãã®ã¯å°ãã¤ãããã®ãããã¾ãã
大æµã®å ´åæ§é ä½ã«ãããã³ã°ããæ¹ãæ±ããããããããã®æ¹æ³ãç´¹ä»ãã¦ãããã¨æãã¾ãã
csvãããã³ã°ã©ã¤ãã©ãªã¯jszwec/csvutilãgocarina/gocsvãããã¾ãããä»åã¯åè
ã使ã£ã¦ããã¾ãã
mapperã®å®ç¾©
User struct { ID int `csv:"id"` Name string `csv:"name"` }
以ä¸ã®user.csv
ãèªã¿è¾¼ã¿ã¾ãã
id, name 1, nakabonne 2, ryo nakao
Unmarshal
var users []User // ãã¤ãåãèªã¿è¾¼ã b, _ := ioutil.ReadFile("user.csv") // ã¦ã¼ã¶ã¼å®ç¾©åã¹ã©ã¤ã¹ã«ãããã³ã° _ := csvutil.Unmarshal(b, &users)
Marshal
// ã¦ã¼ã¶ã¼å®ç¾©åã¹ã©ã¤ã¹ã®ä½æ users := []User{ {ID: 1, Name: "nakabonne"}, {ID: 2, Name: "ryo nakao"}, } // ãã¤ãåã«å¤æ b, err := csvutil.Marshal(users)