Skip to content

Commit 19ec14a

Browse files
authored
Merge pull request #5 from bc-vincent-zhao/update-readme
add caveats about zero value
2 parents 24ade10 + ea66fbc commit 19ec14a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ fmt.Println(test.Qux) //Prints:
4646
fmt.Println(test.Dur) //Prints: 1m0s
4747
```
4848

49+
Caveats
50+
-------
51+
52+
At the moment, the way the default filler checks whether it should fill a struct field or not is by comparing the current field value with the corresponding zero value of that type. This has a subtle implication: the zero value set explicitly by you will get overriden by default value during `SetDefaults()` call. So if you need to set the field to container zero value, you need to set it explicitly AFTER setting the defaults.
53+
54+
Take the basic example in the above section and change it slightly:
55+
```go
56+
57+
example := ExampleBasic{
58+
Bar: 0,
59+
}
60+
defaults.SetDefaults(example)
61+
fmt.Println(example.Bar) //Prints: 33 instead of 0 (which is zero value for int)
62+
63+
example.Bar = 0 // set needed zero value AFTER applying defaults
64+
fmt.Println(example.Bar) //Prints: 0
65+
66+
```
67+
4968
License
5069
-------
5170

0 commit comments

Comments
 (0)