togo install togo-framework/import-export
The togo answer to Laravel Excel / django-import-export: export any rows or a registered resource to CSV/XLSX, and import from CSV/XLSX with column mapping, header detection, and per-row validation that reports row/column errors instead of failing the whole file.
ie, _ := importexport.FromKernel(k) rows := []map[string]any{{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}} csvBytes, _ := ie.Export(ctx, rows, importexport.ExportOpts{Format: "csv", Columns: []string{"id", "name"}}) xlsxBytes, _ := ie.Export(ctx, rows, importexport.ExportOpts{Format: "xlsx", Sheet: "People"})
data := []byte("Identifier,Full Name\n1,Alice\n,Missing\n") rows, rowErrors, err := ie.Import(ctx, data, importexport.ImportOpts{ Format: "csv", Mapping: map[string]string{"Identifier": "id", "Full Name": "name"}, Validate: func(row map[string]any) error { if row["id"] == "" { return errors.New("id is required") } return nil }, }) // rows = valid rows (mapped to id/name); rowErrors = []RowError{ {Row:2, Message:"id is required"} }
importexport.Register(importexport.Resource{ Name: "people", Columns: []string{"id", "name"}, Fetch: func(ctx context.Context) ([]map[string]any, error) { return loadPeople(ctx) }, Insert: func(ctx context.Context, row map[string]any) error { return savePerson(ctx, row) }, }) // then export/import by name via the Go API or REST.
| Method | Path | Description |
|---|---|---|
GET |
/api/export/{resource}?format=csv|xlsx |
Download a registered resource |
POST |
/api/import/{resource}?format=csv|xlsx |
Upload + insert; returns {inserted, errors} |
No required env. XLSX uses excelize; CSV uses the stdlib.