|
|
|
|
|
by tqkxzugoaupvwqr
3586 days ago
|
|
I think the problem they wanted to avoid is incompatibility (Go cares much about compatibility, see Go 1’s Promise of Compatibility[1]). Private fields mean to external packages “This is non of your business. The field can change or be removed at any time without notice.”. If external packages could add methods to a struct, thus turning private fields into effectively-public fields, the whole struct (all its fields) potentially becomes public. How do you maintain a package if your defined API (public fields/methods) is ignored and instead everything is made available to external packages? To keep compatibility, you could never refactor the package, instead you have to keep all private fields to not break external packages that use them. [1] https://golang.org/doc/go1compat |
|
I wish I could add methods from outside of my package. I currently run into cyclical dependency issues when I try and define something like...
func (g Game) GetPlayers() []Player { ... }
func (p Player) GetGame() Game { ... }
While having Game and Player in separate packages. It's not the end of the world having them in the same package, but the amount of methods does pile up...