6 ms·
The funny thing is that if you want to create an immutable type in Java - no problem, just create a constructor and mark fields as final[1]. In Go - you can't
by ickyforce 7y ago
The funny thing is that if you want to create an immutable type in Java - no problem, just create a constructor and mark fields as final[1].
In Go - you can't do it like that. You need those getters, preferably an interface (because exported struct allows anybody to create a zero-value) and a factory function.
[1] - assuming their types are also immutable
- throwaway894345 7y agoImmutability is neat, but you can get a long way by passing by copy and/or documenting whether a value shouldn't be mutated. I would like to see immutability added to Go, but it would break my heart if it was inspired by Java (i.e., final) instead of Rust's notion of immutability.