You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Preprocessor functions will be called before cell values are decoded and validator functions will be called after that.
53
55
56
+
[Playground](https://go.dev/play/p/Ndn2sj4030c)
57
+
54
58
```go
55
59
data:= []byte(`
56
60
name,age,address
@@ -95,6 +99,8 @@ tom ,26,new york`)
95
99
96
100
- When set `StopOnError = false`, the decoding will continue to process the data even when errors occur. You can handle all errors of the process at once.
97
101
102
+
[Playground](https://go.dev/play/p/mV1UX69mHWS)
103
+
98
104
```go
99
105
data:= []byte(`
100
106
name,age,address
@@ -130,6 +136,8 @@ tom,26,new york`)
130
136
- Optional column is a column which is defined in the struct tag but not exist in the input data
131
137
- Unrecognized column is a column which exists in the input data but is not defined in the struct tag
132
138
139
+
[Playground](https://go.dev/play/p/OA8Jh3Y4aaD)
140
+
133
141
```go
134
142
data:= []byte(`
135
143
name,age,mark
@@ -165,6 +173,8 @@ tom,26,9`)
165
173
166
174
- By default, the header order in the input data must match the order defined in the struct tag.
167
175
176
+
[Playground](https://go.dev/play/p/3va5HYWacPO)
177
+
168
178
```go
169
179
// `address` appears before `age`
170
180
data:= []byte(`
@@ -200,6 +210,8 @@ tom,new york,9`)
200
210
201
211
- Fixed inline columns are represented by an inner struct. `prefix` can be set for inline columns.
202
212
213
+
[Playground](https://go.dev/play/p/ouZDv4zjfSA)
214
+
203
215
```go
204
216
data:= []byte(`
205
217
name,age,mark_math,mark_chemistry,mark_physics
@@ -245,6 +257,8 @@ tom,19,7,8,9`)
245
257
246
258
- Dynamic inline columns are represented by an inner struct with variable number of columns. `prefix` can also be set for them.
247
259
260
+
[Playground](https://go.dev/play/p/5SCRzMp6NZP)
261
+
248
262
```go
249
263
data:= []byte(`
250
264
name,age,mark_math,mark_chemistry,mark_physics
@@ -282,6 +296,8 @@ tom,19,7,8,9`)
282
296
283
297
- Any user custom type can be decoded by implementing either `encoding.TextUnmarshaler` or `csvlib.CSVUnmarshaler` or both. `csvlib.CSVUnmarshaler` has higher priority.
284
298
299
+
[Playground](https://go.dev/play/p/g_QmAN6di-9)
300
+
285
301
```go
286
302
typeBirthDate time.Time
287
303
@@ -329,6 +345,8 @@ tom,1989-11-11`)
329
345
330
346
- By default, the decoder detects columns via comma delimiter, if you want to decode custom one, use `csv.Reader` from the built-in package `encoding/csv` as the input reader.
331
347
348
+
[Playground](https://go.dev/play/p/HgxcyzeHho6)
349
+
332
350
```go
333
351
data:= []byte(
334
352
"name\tage\taddress\n" +
@@ -361,6 +379,8 @@ tom,1989-11-11`)
361
379
362
380
### Decode one-by-one
363
381
382
+
[Playground](https://go.dev/play/p/81jLLnGbENa)
383
+
364
384
```go
365
385
data:= []byte(`
366
386
name,age,address
@@ -393,6 +413,8 @@ tom,26,new york`)
393
413
394
414
- This functionality allows to decode multiple input data with header translated into specific language
395
415
416
+
[Playground](https://go.dev/play/p/Tf-unVfBnYH)
417
+
396
418
```go
397
419
// Sample localization data (you can define them somewhere else such as in a json file)
398
420
var (
@@ -469,6 +491,8 @@ tom,19,new york`)
469
491
470
492
- Decoding errors can be rendered as a more human-readable content such as text or CSV.
Copy file name to clipboardExpand all lines: docs/ENCODING.md
+21-1
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,8 @@
16
16
17
17
### First example
18
18
19
+
[Playground](https://go.dev/play/p/SVTP87loZ-g)
20
+
19
21
```go
20
22
typeStudentstruct {
21
23
Namestring`csv:"name"`
@@ -41,6 +43,8 @@
41
43
42
44
### No header mode
43
45
46
+
[Playground](https://go.dev/play/p/qkfXClVUO5e)
47
+
44
48
```go
45
49
typeStudentstruct {
46
50
Namestring`csv:"name"`
@@ -69,6 +73,8 @@
69
73
70
74
- Postprocessor functions will be called after Go values are encoded into CSV string.
71
75
76
+
[Playground](https://go.dev/play/p/MlppfYm-xey)
77
+
72
78
```go
73
79
typeStudentstruct {
74
80
Namestring`csv:"name"`
@@ -100,6 +106,8 @@
100
106
101
107
- To skip encoding a column, you can omit the `csv` tag from the struct field or use `csv:"-"`. You can also use an equivalent configuration option for the column.
102
108
109
+
[Playground](https://go.dev/play/p/NHzpm9o-y6N)
110
+
103
111
```go
104
112
typeStudentstruct {
105
113
Namestring`csv:"name"`
@@ -131,6 +139,8 @@
131
139
132
140
- Fixed inline columns are represented by an inner struct. `prefix` can be set for inline columns.
133
141
142
+
[Playground](https://go.dev/play/p/kenlqLQ9p75)
143
+
134
144
```go
135
145
typeMarksstruct {
136
146
Mathint`csv:"math"`
@@ -164,6 +174,8 @@
164
174
165
175
- Dynamic inline columns are represented by an inner struct with variable number of columns. `prefix` can also be set for them.
166
176
177
+
[Playground](https://go.dev/play/p/BaMNjFj5Kr0)
178
+
167
179
```go
168
180
typeStudentstruct {
169
181
Namestring`csv:"name"`
@@ -193,11 +205,13 @@
193
205
194
206
- Any user custom type can be decoded by implementing either `encoding.TextMarshaler` or `csvlib.CSVMarshaler` or both. `csvlib.CSVMarshaler` has higher priority.
- By default, the encoder uses comma as the delimiter, if you want to encode custom one, use `csv.Writer` from the built-in package `encoding/csv` as the input writer.
0 commit comments