CSV Module (Headless)

CSV Module (Headless)

Headless (no GUI) example demonstrating every function in the csv module. Uses only the core package — no Fyne/GUI dependencies.

Features

  • csv.parse(text) - First row becomes headers; returns a list of maps
  • csv.parse(text, {header: false}) - Returns raw rows as a list of lists
  • csv.parse(text, {delimiter: ";"}) - Custom field delimiter
  • csv.format(rows) - Encode a list of maps (columns sorted alphabetically)
  • csv.format(rows, {columns: [...]}) - Control column order / subset
  • csv.format(rows, {header: false}) - Omit the header row
  • csv.write(path, rows) / csv.read(path) - File round-trip

Code

require(["v0.8", "@csv"])

// csv.parse — first row becomes headers, returns a list of maps (the default)
let people = csv.parse("name,age,city\nAda,36,London\nBob,40,Paris")
people.each(row => print("  " + row["name"] + " is " + row["age"] + " (" + row["city"] + ")"))

// {header: false} — returns a list of lists (raw rows)
let grid = csv.parse("a,b,c\n1,2,3\n4,5,6", {header: false})

// Custom delimiter
let semi = csv.parse("id;label\n1;alpha\n2;beta", {delimiter: ";"})

// csv.format — list of maps, columns sorted alphabetically by default
print(csv.format(people))

// Control column order / subset
print(csv.format(people, {columns: ["city", "name"]}))

// Omit the header row
print(csv.format(people, {columns: ["name", "age", "city"], header: false}))

// A list of lists is written verbatim
let rows = [["x", "y"], ["1", "2"], ["3", "4"]]
print(csv.format(rows, {delimiter: ";"}))

// csv.write then csv.read — round-trip through a file
let path = "people-out.csv"
csv.write(path, people)
let reloaded = csv.read(path)
print("  wrote and re-read " + string(len(reloaded)) + " rows from " + path)

Enable the module in the Go host with core.WithCSV() and declare it in scripts with require(["@csv"]).

Running

cd examples/38-csv-headless
go run main.go

The file round-trip writes people-out.csv in the current directory.

Full Source

View full example on GitHub ↗