This is a very good question because it delves into what, I suspect, may be an omission from Go, an unchanging state.
From a link to the language , "constant expressions can only contain constant operands and are evaluated at compile time."
You cannot make wars permanent, which is a shame. Answer Joe offers encapsulation as a solution that will work well, but it is verbose, tedious, and can lead to errors.
In comparison, many impure functional languages combine mutable variables with immutable values with a single assignment. For example, Scala has the keywords "val" and "var"; the value of Scala 'var' is very similar to Go 'var'. Invariance is a useful tool in the toolbar because link-transparent functions that are free from side effects can be written along with state-change code. Both have their place. Consistency is also a valuable tool for concurrency, because there is no problem regarding the possible conditions of a race if fixed values are shared between goroutines.
Thus, in my opinion, among many of his strengths, this is one of Go's drawbacks. Apparently, it would not be easy to maintain shafts, as well as vars, the difference being that the compiler checks that each val is assigned exactly once.
Until this feature is added, you have encapsulation as the only option.
source share