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.
Code
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
CSV to TSV ConverterParse RFC 4180 CSV with quoted fields and re-serialize using a tab, pipe, semicolon, or custom delimiter, with optional quoting.ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.ASCII85 EncoderEncode and decode ASCII85 (Base85), the Adobe variant with z zero-compression, handling arbitrary bytes and Unicode.Base32 Encoder & DecoderEncode and decode RFC 4648 Base32 with padding, fully UTF-8 safe so Unicode text round-trips without corruption. Nothing leaves your browser.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.