mcuadros / go-defaults

Go structures with default values using tags

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

pavankumar-bit opened this issue · 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: &{[]}

	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