Skip to content

Commit

Permalink
Improve readme
Browse files Browse the repository at this point in the history
  • Loading branch information
creasty committed Jun 21, 2019
1 parent edf4f6a commit c58e679
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defaults
[![GitHub release](https://img.shields.io/github/release/creasty/defaults.svg)](https://github.com/creasty/defaults/releases)
[![License](https://img.shields.io/github/license/creasty/defaults.svg)](./LICENSE)

Initialize members in a struct with a default value
Initialize structs with default values

- Supports almost all kind of types
- Scalar types
Expand Down Expand Up @@ -36,27 +36,27 @@ type Sample struct {
Gender Gender `default:"m"`

Slice []string `default:"[]"`
SliceByJSON []int `default:"[1, 2, 3]"` // Supports JSON format
SliceByJSON []int `default:"[1, 2, 3]"` // Supports JSON
Map map[string]int `default:"{}"`
MapByJSON map[string]int `default:"{\"foo\": 123}"`

Struct OtherStruct `default:"{}"`
StructPtr *OtherStruct `default:"{\"Foo\": 123}"`

NoTag OtherStruct // Recurses into a nested struct even without a tag
NoTag OtherStruct // Recurses into a nested struct by default
OptOut OtherStruct `default:"-"` // Opt-out
}

type OtherStruct struct {
Hello string `default:"world"` // Tags in a nested struct also work
Foo string `default:"-"`
Foo int `default:"-"`
Random int `default:"-"`
}

// SetDefaults implements defaults.Setter interface
func (s *OtherStruct) SetDefaults() {
if (defaults.CanUpdate(s.Random)) { // Check if the value is initial (recommended)
s.Random = rand.Int() // Set a value dynamically
if defaults.CanUpdate(s.Random) { // Check if it's a zero value (recommended)
s.Random = rand.Int() // Set a dynamic value
}
}
```
Expand Down

0 comments on commit c58e679

Please sign in to comment.