Encode

Write SuperCSV files from Go — build a header, supply rows, and output a valid .supr file.

Install

Go SDK

Add the SuperCSV module to your project, then import the supr package.

go get github.com/supercsv/supercsv
import "github.com/supercsv/supercsv/supr"
Quick

Encode a File

Define a header with typed columns, build your rows as []any slices, and write the whole file in one call.

header := supr.NewHeader([]supr.Column{
    {Name: "id", Type: supr.Int},
    {Name: "name", Type: supr.String},
    {Name: "score", Type: supr.Float},
    {Name: "tags", Type: supr.List(supr.String)},
})

rows := [][]any{
    {int64(1), "Alice", 95.5, []any{"math", "sci"}},
    {int64(2), "Bob", 87.3, []any{"art"}},
}

f, _ := os.Create("output.supr")
defer f.Close()

err := supr.EncodeFile(f, &supr.File{
    Header: header, Rows: rows,
})
Streaming

Row-by-Row

For large datasets, stream rows one at a time. Write the header first, then each row, then close.

f, _ := os.Create("output.supr")
defer f.Close()

enc := supr.NewFileEncoder(header, f)
enc.WriteHeader()

for _, row := range rows {
    enc.WriteRow(row)
}
enc.Close()
Rows

Building Rows

Each row is a []any with one element per column. Use the correct Go types — int64 for ints, float64 for floats, nil for nulls.

row := []any{
    int64(42),                           // int
    3.14,                                // float
    "hello",                             // string
    true,                                // bool
    nil,                                 // null
    []any{int64(1), int64(2), int64(3)}, // list<int>
    [][]any{                             // arr<float>[2,3]
        {1.0, 2.0, 3.0},
        {4.0, 5.0, 6.0},
    },
}

Special Types

Enums

Encoding Enums

Enums accept either the label string or the 0-based index. Both forms produce valid output.

# for enum(red, green, blue):

row := []any{"red"}    // by label
row := []any{int64(0)} // by index (0 = red)
Nulls

Encoding Nulls

Use nil for any nullable field, including containers. Null is explicit in SuperCSV — it's never ambiguous.

row := []any{nil, nil, nil} // all fields null
Temporal

Dates, Times & More

Temporal types and decimals accept string values in their canonical format.

row := []any{
    "2024-01-15",               // date
    "14:30:00",                 // time
    "2024-01-15T14:30:00Z",     // timestamp
    "P1Y2M3D",                  // duration
    "123.456",                  // decimal
}