https://github.com/lzambarda/goflat
Context-aware generic CSV marshaller/unmarshaller
https://github.com/lzambarda/goflat
csv go marshaller unmarshaller
Last synced: about 1 month ago
JSON representation
Context-aware generic CSV marshaller/unmarshaller
- Host: GitHub
- URL: https://github.com/lzambarda/goflat
- Owner: lzambarda
- License: mit
- Created: 2024-11-26T10:50:07.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-09-13T13:48:33.000Z (6 months ago)
- Last Synced: 2025-09-13T15:44:22.493Z (6 months ago)
- Topics: csv, go, marshaller, unmarshaller
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 11
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# goflat

[](https://pkg.go.dev/github.com/lzambarda/goflat)
[](https://goreportcard.com/report/github.com/lzambarda/goflat)
Generic, context-aware flat file marshaller and unmarshaller using the `flat` field tag in structs.
## Overview
```go
type Record struct {
FirstName string `flat:"first_name"`
LastName *string `flat:"last_name"`
Age int `flat:"age"`
Height float32 `flat:"-"` // ignored
}
...
goflat.MarshalIteratorToWriter[Record](ctx, seq, writer *csv.Writer, opts Options)
// or
goflat.MarshalSliceToWriter[Record](ctx, slice, csvWriter, options)
// or
goflat.MarshalChannelToWriter[Record](ctx, inputCh, csvWriter, options)
```
Will result in:
```
first_name,last_name,age
John,Doe,30
Jane,Doe,20
```
Can unmarshal too!
```go
goflat.UnmarshalToChan[Record](ctx, csvReader, options, outputCh)
```
## Options
Both marshal and unmarshal operations support `goflat.Options`, which allow to introduce automatic safety checks, such as duplicated headers, `flat` tag coverage and more.
## Custom marshal / unmarshal
Both operations can be customised for each field in a struct by having that value implementing `goflat.Marshal` and/or `goflat.Unmarshal`.
```go
type Record struct {
Field MyType `flat:"field"`
}
type MyType struct {
Value int
}
func (m *MyType) Marshal() (string,error) {
if m.Value %2 == 0 {
return "odd", nil
}
return "even", nil
}
```