defaults icon indicating copy to clipboard operation
defaults copied to clipboard

Default value for slice of struct doesn't work for golang v 1.12

Open pavankumar-bit opened this issue 4 years ago • 2 comments

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: &{[]}

pavankumar-bit avatar Oct 11 '19 05:10 pavankumar-bit

default for slice is nil, that is right

sdghchj avatar Jun 09 '20 03:06 sdghchj

// Doesn't even work with:
type Parent struct {
	children []Child `default:"[]"`
}

// or this:
type Parent struct {
	children []Child `default:"[{}]"`
}

creasty avatar Oct 06 '21 11:10 creasty