Skip to content

Commit 33dafc1

Browse files
authored
Merge pull request #16 from tiendc/update-docs
Update guideline
2 parents 23695a8 + 7634009 commit 33dafc1

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[![Go Version][gover-img]][gover] [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] [![GoReport][rpt-img]][rpt]
22

3-
# High level CSV lib for Go 1.18+
3+
# High level CSV library for Go 1.18+
44

55
This is a library for decoding and encoding CSV at high level as it provides convenient methods and many configuration options to process the data the way you expect.
66

7+
This library is inspired by the project https://github.com/jszwec/csvutil.
8+
79
## Functionalities
810

911
**Decoding**

docs/DECODING.md

+30
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Dynamic inline columns](#dynamic-inline-columns)
1111
- [Custom unmarshaler](#custom-unmarshaler)
1212
- [Custom column delimiter](#custom-column-delimiter)
13+
- [Decode one-by-one](#decode-one-by-one)
1314
- [Header localization](#header-localization)
1415
- [Render error as human-readable format](#render-error-as-human-readable-format)
1516

@@ -358,6 +359,35 @@ tom,1989-11-11`)
358359
// {Name:tom Age:9 Address:new york}
359360
```
360361

362+
### Decode one-by-one
363+
364+
```go
365+
data := []byte(`
366+
name,age,address
367+
jerry,20,
368+
tom,26,new york`)
369+
370+
type Student struct {
371+
Name string `csv:"name"`
372+
Age int `csv:"age"`
373+
Address string `csv:"address"`
374+
}
375+
376+
reader := csv.NewReader(bytes.NewReader(data))
377+
decoder := csvlib.NewDecoder(reader)
378+
var student Student
379+
for decoder.DecodeOne(&student) != csvlib.ErrFinished {
380+
fmt.Printf("%+v\n", student)
381+
}
382+
383+
fmt.Printf("%+v\n", *decoder.Finish())
384+
385+
// Output:
386+
// {Name:jerry Age:20 Address:}
387+
// {Name:tom Age:26 Address:new york}
388+
// {totalRow:3 unrecognizedColumns:[] missingOptionalColumns:[]}
389+
```
390+
361391
### Header localization
362392

363393
- This functionality allows to decode multiple input data with header translated into specific language

docs/ENCODING.md

+34
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Dynamic inline columns](#dynamic-inline-columns)
1010
- [Custom marshaler](#custom-marshaler)
1111
- [Custom column delimiter](#custom-column-delimiter)
12+
- [Encode one-by-one](#encode-one-by-one)
1213
- [Header localization](#header-localization)
1314

1415
## Content
@@ -255,6 +256,39 @@ func (d BirthDate) MarshalCSV() ([]byte, error) {
255256
// tom 19 new york
256257
```
257258

259+
### Encode one-by-one
260+
261+
```go
262+
type Student struct {
263+
Name string `csv:"name"`
264+
Age int `csv:"age"`
265+
Address string `csv:"address"`
266+
}
267+
268+
students := []Student{
269+
{Name: "jerry", Age: 20, Address: "tokyo"},
270+
{Name: "tom", Age: 19, Address: "new york"},
271+
}
272+
var buf bytes.Buffer
273+
writer := csv.NewWriter(&buf)
274+
encoder := csvlib.NewEncoder(writer)
275+
276+
for _, student := range students {
277+
if err := encoder.EncodeOne(student); err != nil {
278+
fmt.Println("error:", err)
279+
break
280+
}
281+
}
282+
encoder.Finish()
283+
writer.Flush()
284+
fmt.Println(buf.String())
285+
286+
// Output:
287+
// name,age,address
288+
// jerry,20,tokyo
289+
// tom,19,new york
290+
```
291+
258292
### Header localization
259293

260294
- This functionality allows to encode CSV data with header translated into a specific language.

0 commit comments

Comments
 (0)