There is the type Company, which is a struct, containing a map of Person's, which are also all structs.
type Company struct {
employees map[int]Person
}
type Person struct {
[...]
}
After allocating some Person's to the employees-map, I'm trying to call a pointer method on each of these.
func (company *Company) Populate(names []string) {
for i := 1; i <= 15; i++ {
company.employees[i] = Person{names[i - 1], [...]}
company.employees[i].Initialize()
}
}
This miserably fails, with the go-Compiler complaining that I can't call pointer methods on company.employees[i], as well as I can't take the address of company.employees[i]. However, setting the Initialize-method to a non-pointer method and letting it return the copy of the person, and allocating it to the map again by using
company.employees[i] = company.employees[i].Initialize()
works, which is not that different.
Not having worked with pointers ever this bugs me quite much. Map's aren't immutable, and they get modified either way, so calling a pointer method on an entity in a map shouldn't be a problem - atleast in my head.
If anyone could explain to me what I'm doing wrong here - or correct my thinking - I'd be pleased.