C++ avoids allocating memory for class methods every time an instance is created. My gut feeling is to assume that Go also mitigates this kind of duplication. Just to confirm, does Go store the method set of a custom struct only once?
type Custom struct {
value string
}
func (c Custom) TurnItUp() {
c.value = "up"
}
func (c Custom) TurnItDown() {
c.value = "down"
}
... // Many more methods defined for Custom.
// (Positive and negative directions in 100 dimensions)
func main() {
var many []Custom
fmt.Println("Memory: ", foo.memory()) // Measure memory used.
for i := 0; i < 10000; i++ {
append(many, Custom{value: "nowhere"})
}
fmt.Println("Memory: ", foo.memory()) // Measure memory used.
}