go-defaults icon indicating copy to clipboard operation
go-defaults copied to clipboard

Default value for slice of struct isn't working for me

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

package main

import (
	"fmt"

	"gopkg.in/mcuadros/go-defaults.v1"
)

func main() {

	foo := &Parent{}
	defaults.SetDefaults(foo)
	fmt.Print(foo)
}

type Child struct {
	Name string
	Age  int `default:"10"`
}

type Parent struct {
	Children []Child
}

output: &{[]}

pavankumar-bit avatar Oct 10 '19 16:10 pavankumar-bit

	foo := &Parent{
		Children: []Child{{}},
	}

probably you need to write like that to set the default value for ONE Child{} of Children cause Children is a slice @pavankumar-bit

huty1998 avatar Jul 11 '23 05:07 huty1998