CSV Custom Delimiter Semicolon Records

Point csv.Reader and csv.Writer at a semicolon delimiter with the Comma field, so European dialect files roundtrip without any manual splitting.

GoCli Tooling

Code

snippet
package main

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

func main() {
	// 欧洲风格分号分隔:Reader 的 Comma 字段决定字段边界
	src := "name;region;replicas\norders;eu-west;3\nbilling;us-east;1\n"
	reader := csv.NewReader(strings.NewReader(src))
	reader.Comma = ';'

	records, err := reader.ReadAll()
	if err != nil {
		fmt.Println("read:", err)
		return
	}
	for _, row := range records {
		fmt.Println(row)
	}

	// Writer 用同一 Comma 写回,保持分号方言
	var out bytes.Buffer
	writer := csv.NewWriter(&out)
	writer.Comma = ';'
	if err := writer.WriteAll(records); err != nil {
		fmt.Println("write:", err)
		return
	}
	writer.Flush()
	fmt.Printf("roundtrip:\n%s", out.String())
}

Order of operations

CSV Custom Delimiter Semicolon Records comes down to the function main.

A single loop drives the file — for _, row := range records. Error handling is inline: 2 places test err != nil and branch immediately.

What to watch for

  • Nothing from a package registry — the language’s built-in APIs carry it.

Using it

Signature: func main()

The demo output is labeled (read:, write:, roundtrip:\n); each label names the call that printed it.

Key statements

for _, row := range records {
	fmt.Println(row)
}
if err != nil {
	fmt.Println("read:", err)
	return
}

Interactive Tools for This Topic

FAQ

What does CSV Custom Delimiter Semicolon Records print when run?
Execute the whole file; each printed line is prefixed with a label like `read:`, `write:`, `roundtrip:\n`.