LoginSignup
0
0

アドベントカレンダー10日目。
csvファイルの扱いについてです。

以下のCSVファイルを用意しておきます。

cat app/day10/sample.csv
Name,Age,City
Alice,25,New York
Bob,30,Los Angeles
Charlie,35,Chicago

読み取り

package main

import (
	"encoding/csv"
	"fmt"
	"os"
)

func main() {
	file, err := os.Open("app/day10/sample.csv")
	if err != nil {
		return
	}
	defer file.Close()

	r := csv.NewReader(file)
	for {
		record, err := r.Read()
		if err != nil {
			return
		}
		fmt.Println(record)
	}
}
$ go run app/day10/main.go
[Name Age City]
[Alice 25 New York]
[Bob 30 Los Angeles]
[Charlie 35 Chicago]

思ったより短い記述で読み取りができました。
リストとして出力されています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0