0Pricing
Go Academy · Lesson

encoding/csv

Read and write CSV.

encoding/csv is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is CSV

CSV (comma-separated values) stores tabular data as plain text, one record per line, fields separated by commas. Go's encoding/csv package reads and writes it.

Reading from a String

Create a reader from any io.Reader. strings.NewReader wraps a string so we can demo without a file.

package main

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

func main() {
	r := csv.NewReader(strings.NewReader("a,b,c"))
	rec, _ := r.Read()
	fmt.Println(rec)
}

Reading All Records

ReadAll reads every row into a [][]string. Each inner slice is one record's fields.

package main

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

func main() {
	data := "name,age\nAnn,30\nBob,25"
	r := csv.NewReader(strings.NewReader(data))
	records, _ := r.ReadAll()
	fmt.Println(records)
}

Reading Row by Row

For large files, read one record at a time with Read. It returns io.EOF when done.

package main

import (
	"encoding/csv"
	"fmt"
	"io"
	"strings"
)

func main() {
	r := csv.NewReader(strings.NewReader("a,1\nb,2"))
	for {
		rec, err := r.Read()
		if err == io.EOF {
			break
		}
		fmt.Println(rec)
	}
}

Accessing Fields

Each record is a slice of strings. Index into it to read a column.

package main

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

func main() {
	r := csv.NewReader(strings.NewReader("Ann,30"))
	rec, _ := r.Read()
	fmt.Println("name:", rec[0], "age:", rec[1])
}

Custom Delimiter

Set r.Comma to use a different separator, such as a semicolon or tab.

package main

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

func main() {
	r := csv.NewReader(strings.NewReader("a;b;c"))
	r.Comma = ';'
	rec, _ := r.Read()
	fmt.Println(rec)
}

Writing CSV

csv.NewWriter writes records. Call Flush to push buffered data out.

package main

import (
	"encoding/csv"
	"os"
)

func main() {
	w := csv.NewWriter(os.Stdout)
	w.Write([]string{"name", "age"})
	w.Write([]string{"Ann", "30"})
	w.Flush()
}

Writing Many Rows

WriteAll writes a whole [][]string at once and flushes automatically.

package main

import (
	"encoding/csv"
	"os"
)

func main() {
	w := csv.NewWriter(os.Stdout)
	rows := [][]string{{"a", "1"}, {"b", "2"}}
	w.WriteAll(rows)
}

Automatic Quoting

The writer quotes fields that contain commas, quotes, or newlines, so the output stays valid CSV.

package main

import (
	"encoding/csv"
	"os"
)

func main() {
	w := csv.NewWriter(os.Stdout)
	w.Write([]string{"hello, world", "plain"})
	w.Flush()
}

Don't Forget Flush

The writer buffers data. If you forget Flush (and do not use WriteAll), some rows may never be written. Always flush before the program ends.

Handling Errors

After flushing, check w.Error() to catch any write failures that occurred during buffering.

package main

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

func main() {
	w := csv.NewWriter(os.Stdout)
	w.Write([]string{"x", "y"})
	w.Flush()
	fmt.Fprintln(os.Stderr, w.Error())
}

Quick Check

What does a CSV reader's ReadAll return for the parsed data?

Recap

encoding/csv:

  • csv.NewReader with Read or ReadAll
  • csv.NewWriter with Write/WriteAll, then Flush
  • Set Comma for custom delimiters; quoting is automatic

Frequently asked questions

Is the “encoding/csv” lesson free?

Yes — the full text of “encoding/csv” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “encoding/csv”?

Read and write CSV. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “encoding/csv” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. encoding/json Recap
  2. encoding/xml
  3. encoding/csv
  4. gob and Binary Encoding
← Back to Go Academy