This question already has an answer here:
I am new to Golang. I have this simple code, that I can't get to work; the problem is that after call the method LoadGroups
, the "main" function doesn't see the changes:
package main
import "fmt"
type Group struct {
Name string
}
type Configuration struct {
Groups []Group
}
func NewConfiguration() (error, *Configuration) {
conf := Configuration{}
conf.LoadGroups()
fmt.Print("Final number of groups: ", len(conf.Groups))
return nil, &conf
}
func (conf Configuration) LoadGroups() {
for i := 0; i < 5; i++ {
conf.Groups = append(conf.Groups, Group{Name: "Group " + string(i)})
fmt.Println("Current number of groups: ", len(conf.Groups))
}
}
func main() {
NewConfiguration()
}
Playground: https://play.golang.org/p/VyneKpjdA-
</div>