So I am trying to write some code that allows me to edit values in an array in a struct. This example uses "Status" as a possible value to alter, but this is just a simplification to try to get my intent across.
package main
import(
"fmt"
)
type Parent struct {
Children []Child
}
type Child struct {
Status string
}
func (p *Parent) Add() *Child {
var child Child
child.Status = "1"
p.Children = append(p.Children, child)
return &p.Children[len(p.Children)-1]
}
func main() {
var p Parent
child := p.Add()
child.Status = "2"
fmt.Println(p)
fmt.Println(child)
}
This doesn't feel "proper". How should I do this in golang? Certainly I could pass the value in as a parameter, but in my particular case I would like to edit function pointers that are inside the Child struct (not in this code to keep it short) after having added the child. That feels nicer, but maybe I just need to pass them as parameters to the Add method?
eg
func (p *Parent) Add(fn1 func(), fn2 func()) *Child {
Anyway just wondering if anybody has any thoughts on this type of situation.