File tree 3 files changed +67
-1
lines changed
3 files changed +67
-1
lines changed Original file line number Diff line number Diff line change 1
1
[ ![ Go Version] [ gover-img ]] [ gover ] [ ![ GoDoc] [ doc-img ]] [ doc ] [ ![ Build Status] [ ci-img ]] [ ci ] [ ![ Coverage Status] [ cov-img ]] [ cov ] [ ![ GoReport] [ rpt-img ]] [ rpt ]
2
2
3
- # High level CSV lib for Go 1.18+
3
+ # High level CSV library for Go 1.18+
4
4
5
5
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.
6
6
7
+ This library is inspired by the project https://github.com/jszwec/csvutil .
8
+
7
9
## Functionalities
8
10
9
11
** Decoding**
Original file line number Diff line number Diff line change 10
10
- [ Dynamic inline columns] ( #dynamic-inline-columns )
11
11
- [ Custom unmarshaler] ( #custom-unmarshaler )
12
12
- [ Custom column delimiter] ( #custom-column-delimiter )
13
+ - [ Decode one-by-one] ( #decode-one-by-one )
13
14
- [ Header localization] ( #header-localization )
14
15
- [ Render error as human-readable format] ( #render-error-as-human-readable-format )
15
16
@@ -358,6 +359,35 @@ tom,1989-11-11`)
358
359
// {Name:tom Age:9 Address:new york}
359
360
```
360
361
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
+
361
391
### Header localization
362
392
363
393
- This functionality allows to decode multiple input data with header translated into specific language
Original file line number Diff line number Diff line change 9
9
- [ Dynamic inline columns] ( #dynamic-inline-columns )
10
10
- [ Custom marshaler] ( #custom-marshaler )
11
11
- [ Custom column delimiter] ( #custom-column-delimiter )
12
+ - [ Encode one-by-one] ( #encode-one-by-one )
12
13
- [ Header localization] ( #header-localization )
13
14
14
15
## Content
@@ -255,6 +256,39 @@ func (d BirthDate) MarshalCSV() ([]byte, error) {
255
256
// tom 19 new york
256
257
```
257
258
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
+
258
292
### Header localization
259
293
260
294
- This functionality allows to encode CSV data with header translated into a specific language.
You can’t perform that action at this time.
0 commit comments