Skip to content

Commit 6fd8572

Browse files
committed
Add Go Playground links for examples
1 parent 83412b9 commit 6fd8572

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

docs/DECODING.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
### First example
2020

21+
[Playground](https://go.dev/play/p/tUw9SfaD8QK)
22+
2123
```go
2224
data := []byte(`
2325
name,birthdate,address
@@ -51,6 +53,8 @@ tom,1989-11-11T00:00:00Z,new york`)
5153

5254
- Preprocessor functions will be called before cell values are decoded and validator functions will be called after that.
5355

56+
[Playground](https://go.dev/play/p/Ndn2sj4030c)
57+
5458
```go
5559
data := []byte(`
5660
name,age,address
@@ -95,6 +99,8 @@ tom ,26,new york`)
9599

96100
- 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.
97101

102+
[Playground](https://go.dev/play/p/mV1UX69mHWS)
103+
98104
```go
99105
data := []byte(`
100106
name,age,address
@@ -130,6 +136,8 @@ tom,26,new york`)
130136
- Optional column is a column which is defined in the struct tag but not exist in the input data
131137
- Unrecognized column is a column which exists in the input data but is not defined in the struct tag
132138

139+
[Playground](https://go.dev/play/p/OA8Jh3Y4aaD)
140+
133141
```go
134142
data := []byte(`
135143
name,age,mark
@@ -165,6 +173,8 @@ tom,26,9`)
165173

166174
- By default, the header order in the input data must match the order defined in the struct tag.
167175

176+
[Playground](https://go.dev/play/p/3va5HYWacPO)
177+
168178
```go
169179
// `address` appears before `age`
170180
data := []byte(`
@@ -200,6 +210,8 @@ tom,new york,9`)
200210

201211
- Fixed inline columns are represented by an inner struct. `prefix` can be set for inline columns.
202212

213+
[Playground](https://go.dev/play/p/ouZDv4zjfSA)
214+
203215
```go
204216
data := []byte(`
205217
name,age,mark_math,mark_chemistry,mark_physics
@@ -245,6 +257,8 @@ tom,19,7,8,9`)
245257

246258
- Dynamic inline columns are represented by an inner struct with variable number of columns. `prefix` can also be set for them.
247259

260+
[Playground](https://go.dev/play/p/5SCRzMp6NZP)
261+
248262
```go
249263
data := []byte(`
250264
name,age,mark_math,mark_chemistry,mark_physics
@@ -282,6 +296,8 @@ tom,19,7,8,9`)
282296

283297
- Any user custom type can be decoded by implementing either `encoding.TextUnmarshaler` or `csvlib.CSVUnmarshaler` or both. `csvlib.CSVUnmarshaler` has higher priority.
284298

299+
[Playground](https://go.dev/play/p/g_QmAN6di-9)
300+
285301
```go
286302
type BirthDate time.Time
287303

@@ -329,6 +345,8 @@ tom,1989-11-11`)
329345

330346
- 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.
331347

348+
[Playground](https://go.dev/play/p/HgxcyzeHho6)
349+
332350
```go
333351
data := []byte(
334352
"name\tage\taddress\n" +
@@ -361,6 +379,8 @@ tom,1989-11-11`)
361379

362380
### Decode one-by-one
363381

382+
[Playground](https://go.dev/play/p/81jLLnGbENa)
383+
364384
```go
365385
data := []byte(`
366386
name,age,address
@@ -393,6 +413,8 @@ tom,26,new york`)
393413

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

416+
[Playground](https://go.dev/play/p/Tf-unVfBnYH)
417+
396418
```go
397419
// Sample localization data (you can define them somewhere else such as in a json file)
398420
var (
@@ -469,6 +491,8 @@ tom,19,new york`)
469491

470492
- Decoding errors can be rendered as a more human-readable content such as text or CSV.
471493

494+
[Playground](https://go.dev/play/p/cTNGAUlP7s_T)
495+
472496
```go
473497
data := []byte(`
474498
name,age,address
@@ -488,7 +512,7 @@ jj,40,`)
488512
cfg.StopOnError = false
489513
cfg.DetectRowLine = true
490514
cfg.ConfigureColumn("name", func(cfg *csvlib.DecodeColumnConfig) {
491-
cfg.ValidatorFuncs = []csvlib.ValidatorFunc{csvlib.ValidatorRange(1, 10)}
515+
cfg.ValidatorFuncs = []csvlib.ValidatorFunc{csvlib.ValidatorStrLen[string](3, 10)}
492516
cfg.OnCellErrorFunc = func(e *csvlib.CellError) {
493517
if errors.Is(e, csvlib.ErrValidationStrLen) {
494518
e.SetLocalizationKey("Column {{.Column}} - '{{.Value}}': Name length must be from {{.MinLen}} to {{.MaxLen}}")
@@ -521,6 +545,7 @@ jj,40,`)
521545

522546
- Render error as CSV content.
523547

548+
[Playground](https://go.dev/play/p/7sJTKNSrOYl)
524549

525550
```go
526551
renderer, _ := csvlib.NewCSVRenderer(err.(*csvlib.Errors), func(cfg *csvlib.CSVRenderConfig) {

docs/ENCODING.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
### First example
1818

19+
[Playground](https://go.dev/play/p/SVTP87loZ-g)
20+
1921
```go
2022
type Student struct {
2123
Name string `csv:"name"`
@@ -41,6 +43,8 @@
4143

4244
### No header mode
4345

46+
[Playground](https://go.dev/play/p/qkfXClVUO5e)
47+
4448
```go
4549
type Student struct {
4650
Name string `csv:"name"`
@@ -69,6 +73,8 @@
6973

7074
- Postprocessor functions will be called after Go values are encoded into CSV string.
7175

76+
[Playground](https://go.dev/play/p/MlppfYm-xey)
77+
7278
```go
7379
type Student struct {
7480
Name string `csv:"name"`
@@ -100,6 +106,8 @@
100106

101107
- 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.
102108

109+
[Playground](https://go.dev/play/p/NHzpm9o-y6N)
110+
103111
```go
104112
type Student struct {
105113
Name string `csv:"name"`
@@ -131,6 +139,8 @@
131139

132140
- Fixed inline columns are represented by an inner struct. `prefix` can be set for inline columns.
133141

142+
[Playground](https://go.dev/play/p/kenlqLQ9p75)
143+
134144
```go
135145
type Marks struct {
136146
Math int `csv:"math"`
@@ -164,6 +174,8 @@
164174

165175
- Dynamic inline columns are represented by an inner struct with variable number of columns. `prefix` can also be set for them.
166176

177+
[Playground](https://go.dev/play/p/BaMNjFj5Kr0)
178+
167179
```go
168180
type Student struct {
169181
Name string `csv:"name"`
@@ -193,11 +205,13 @@
193205

194206
- Any user custom type can be decoded by implementing either `encoding.TextMarshaler` or `csvlib.CSVMarshaler` or both. `csvlib.CSVMarshaler` has higher priority.
195207

208+
[Playground](https://go.dev/play/p/iSfxQSkUCax)
209+
196210
```go
197211
type BirthDate time.Time
198212

199213
func (d BirthDate) MarshalCSV() ([]byte, error) {
200-
return []byte(time.Time(d).Format("2006-02-01")), nil // RFC3339
214+
return []byte(time.Time(d).Format("2006-01-02")), nil // RFC3339
201215
}
202216
```
203217
```go
@@ -227,6 +241,8 @@ func (d BirthDate) MarshalCSV() ([]byte, error) {
227241

228242
- 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.
229243

244+
[Playground](https://go.dev/play/p/WxOStP5cb6G)
245+
230246
```go
231247
type Student struct {
232248
Name string `csv:"name"`
@@ -258,6 +274,8 @@ func (d BirthDate) MarshalCSV() ([]byte, error) {
258274

259275
### Encode one-by-one
260276

277+
[Playground](https://go.dev/play/p/aQWk3qqlBVm)
278+
261279
```go
262280
type Student struct {
263281
Name string `csv:"name"`
@@ -293,6 +311,8 @@ func (d BirthDate) MarshalCSV() ([]byte, error) {
293311

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

314+
[Playground](https://go.dev/play/p/jH7joZPgWJO)
315+
296316
```go
297317
// Sample localization data (you can define them somewhere else such as in a json file)
298318
var (

0 commit comments

Comments
 (0)