defaults
defaults copied to clipboard
Default value for slice of struct doesn't work for golang v 1.12
package controllers
import (
"fmt"
"github.com/creasty/defaults"
)
func main() {
foo := &Parent{}
if err := defaults.Set(foo); err != nil {
fmt.Println(err)
}
fmt.Print(foo)
}
type Child struct {
Name string
Age int `default:"33"`
}
type Parent struct {
children []Child
}
output: &{[]}
default for slice is nil, that is right
// Doesn't even work with:
type Parent struct {
children []Child `default:"[]"`
}
// or this:
type Parent struct {
children []Child `default:"[{}]"`
}