https://github.com/onozaty/go-customcsv
Customizing the CSV format
https://github.com/onozaty/go-customcsv
csv go golang
Last synced: about 2 months ago
JSON representation
Customizing the CSV format
- Host: GitHub
- URL: https://github.com/onozaty/go-customcsv
- Owner: onozaty
- License: mit
- Created: 2021-06-06T14:44:09.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-10T23:34:26.000Z (over 4 years ago)
- Last Synced: 2024-05-02T05:48:50.542Z (almost 2 years ago)
- Topics: csv, go, golang
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# go-customcsv
[](https://pkg.go.dev/github.com/onozaty/go-customcsv)
[](https://github.com/onozaty/go-customcsv/actions/workflows/test.yaml)
[](https://codecov.io/gh/onozaty/go-customcsv)
This is a library for customizing the CSV format.
You can customize the following.
* (Reader/Writer) Format characters
* Delimiter (default: `,`)
* Quote (default: `"`)
* Record separator (default: `\r\n`)
* (Writer) Always quote (default: `false`)
* (Reader) Verify the number of fields per record (default: Check by the number of fields in the first record)
In Reader, the head BOM will be automatically skipped.
## Usage
### Reader
```go
f, err := os.Open("input.csv")
if err != nil {
return err
}
r := customcsv.NewReader(f)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
fmt.Println(record)
}
```
In `Reader`, the following items can be customized.
```go
type Reader struct {
// Delimiter is the field delimiter.
// It is set to default comma (',') by NewReader.
Delimiter rune
// Quote is the field quote character.
// It is set to default double quote ('"') by NewReader.
Quote rune
// SpecialRecordSeparator is the special record separator.
// If not specified, a newline ('\n' '\r' '\r\n') will be used as the record separator.
SpecialRecordSeparator string
// FieldsPerRecord is the number of expected fields per record.
// FieldsPerRecord > 0 : Checks for the specified value.
// FieldsPerRecord = 0 : Check by the number of fields in the first record.
// FieldsPerRecord < 0 : No check.
FieldsPerRecord int
}
```
Set this after `NewReader()`.
```go
r := customcsv.NewReader(f)
// Customize format
r.Delimiter = ';'
r.SpecialRecordSeparator = "|"
```
### Writer
```go
f, err := os.Create("output.csv")
if err != nil {
return err
}
w := customcsv.NewWriter(f)
for _, record := range records {
if err := w.Write(record); err != nil {
return err
}
}
if err := w.Flush(); err != nil {
return err
}
```
In `Writer`, the following items can be customized.
```go
type Writer struct {
// Delimiter is the field delimiter.
// It is set to default comma (',') by NewWriter.
Delimiter rune
// Quote is the field quote character.
// It is set to default double quote ('"') by NewWriter.
Quote rune
// If True, always quote the fields.
AllQuotes bool
// RecordSeparator is the record separator.
// It is set to default CRLF ('\r\n') by NewWriter.
RecordSeparator string
}
```
Set this after `NewWriter()`.
```go
w := customcsv.NewWriter(f)
// Customize format
w.AllQuotes = true
w.RecordSeparator = "\n"
```
## License
MIT
## Author
[onozaty](https://github.com/onozaty)