@@ -30,27 +30,39 @@ Usage
3030-----
3131
3232``` go
33+ package main
34+
35+ import (
36+ " encoding/json"
37+ " fmt"
38+ " math/rand"
39+
40+ " github.com/creasty/defaults"
41+ )
42+
3343type Gender string
3444
3545type Sample struct {
36- Name string ` default:"John Smith"`
37- Age int ` default:"27"`
38- Gender Gender ` default:"m"`
39-
40- Slice []string ` default:"[]"`
41- SliceByJSON []int ` default:"[1, 2, 3]"` // Supports JSON
42-
43- Map map [string ]int ` default:"{}"`
44- MapByJSON map [string ]int ` default:"{\"foo\": 123}"`
45- MapOfStruct map [string ]OtherStruct
46- MapOfPtrStruct map [string ]*OtherStruct
47- MapOfStructWithTag map [string ]OtherStruct ` default:"{\"Key1\": {\"Foo\":123}}"`
48-
49- Struct OtherStruct ` default:"{}"`
46+ Name string ` default:"John Smith"`
47+ Age int ` default:"27"`
48+ Gender Gender ` default:"m"`
49+ Working bool ` default:"true"`
50+
51+ SliceInt []int ` default:"[1, 2, 3]"`
52+ SlicePtr []*int ` default:"[1, 2, 3]"`
53+ SliceString []string ` default:"[\"a\", \"b\"]"`
54+
55+ MapNull map [string ]int ` default:"{}"`
56+ Map map [string ]int ` default:"{\"key1\": 123}"`
57+ MapOfStruct map [string ]OtherStruct ` default:"{\"Key2\": {\"Foo\":123}}"`
58+ MapOfPtrStruct map [string ]*OtherStruct ` default:"{\"Key3\": {\"Foo\":123}}"`
59+ MapOfStructWithTag map [string ]OtherStruct ` default:"{\"Key4\": {\"Foo\":123}}"`
60+
61+ Struct OtherStruct ` default:"{\"Foo\": 123}"`
5062 StructPtr *OtherStruct ` default:"{\"Foo\": 123}"`
5163
52- NoTag OtherStruct // Recurses into a nested struct by default
53- OptOut OtherStruct ` default:"-"` // Opt-out
64+ NoTag OtherStruct // Recurses into a nested struct by default
65+ NoOption OtherStruct ` default:"-"` // no option
5466}
5567
5668type OtherStruct struct {
@@ -65,11 +77,84 @@ func (s *OtherStruct) SetDefaults() {
6577 s.Random = rand.Int () // Set a dynamic value
6678 }
6779}
68- ```
6980
70- ``` go
71- obj := &Sample{}
72- if err := defaults.Set (obj); err != nil {
73- panic (err)
81+ func main () {
82+ obj := &Sample{}
83+ if err := defaults.Set (obj); err != nil {
84+ panic (err)
85+ }
86+
87+ out , err := json.MarshalIndent (obj, " " , " " )
88+ if err != nil {
89+ panic (err)
90+ }
91+ fmt.Println (string (out))
92+
93+ // Output:
94+ // {
95+ // "Name": "John Smith",
96+ // "Age": 27,
97+ // "Gender": "m",
98+ // "Working": true,
99+ // "SliceInt": [
100+ // 1,
101+ // 2,
102+ // 3
103+ // ],
104+ // "SlicePtr": [
105+ // 1,
106+ // 2,
107+ // 3
108+ // ],
109+ // "SliceString": [
110+ // "a",
111+ // "b"
112+ // ],
113+ // "MapNull": {},
114+ // "Map": {
115+ // "key1": 123
116+ // },
117+ // "MapOfStruct": {
118+ // "Key2": {
119+ // "Hello": "world",
120+ // "Foo": 123,
121+ // "Random": 5577006791947779410
122+ // }
123+ // },
124+ // "MapOfPtrStruct": {
125+ // "Key3": {
126+ // "Hello": "world",
127+ // "Foo": 123,
128+ // "Random": 8674665223082153551
129+ // }
130+ // },
131+ // "MapOfStructWithTag": {
132+ // "Key4": {
133+ // "Hello": "world",
134+ // "Foo": 123,
135+ // "Random": 6129484611666145821
136+ // }
137+ // },
138+ // "Struct": {
139+ // "Hello": "world",
140+ // "Foo": 123,
141+ // "Random": 4037200794235010051
142+ // },
143+ // "StructPtr": {
144+ // "Hello": "world",
145+ // "Foo": 123,
146+ // "Random": 3916589616287113937
147+ // },
148+ // "NoTag": {
149+ // "Hello": "world",
150+ // "Foo": 0,
151+ // "Random": 6334824724549167320
152+ // },
153+ // "NoOption": {
154+ // "Hello": "",
155+ // "Foo": 0,
156+ // "Random": 0
157+ // }
158+ // }
74159}
75160```
0 commit comments