I'm trying to make my Go application more object oriented. Right now I have the following call:
groups.AllGroups = GrowGroupsArray(groups.AllGroups)
Which calls:
func GrowGroupsArray(g []Group) []Group {
newSlice := make([]Group, len(g), 2*cap(g)+1)
copy(newSlice, g)
g = newSlice
return g
}
This technically works but I would rather this:
//groups is of type Groups
//AllGroups is of type []Group
groups.AllGroups.GrowGroupsArray()
func (g Groups) GrowGroupsArray() {
newSlice := make([]Group, len(g), 2*cap(g)+1)
copy(newSlice, g)
g.AllGroups = newSlice
}
This compiles fine but I get a runtime panic because nothing is being saved to the object when the function is completed (out of scope). I've encountered the exact same issue in several places where the first example works but the second will not save the new array to my object. The old array persists after the function has been called. Any help would be greatly appreciated.