Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pavankumar-bit opened this issue Oct 11, 2019 · 2 comments
Open

Comments

@pavankumar-bit
Copy link

pavankumar-bit commented Oct 11, 2019

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

@sdghchj
Copy link
Contributor

sdghchj commented Jun 9, 2020

default for slice is nil, that is right

@creasty
Copy link
Owner

creasty commented Oct 6, 2021

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants