In Go, you can create an immutable variable of type int by declaring it as const
:
main.go20 chars2 lines
Once you have declared a constant, you cannot change its value. If you try to reassign a value to myNumber
, you will get a compilation error.
Note that you can only assign a value to a constant at the time of its declaration. You cannot declare a constant and then assign a value to it later:
main.go66 chars3 lines
Declaring a constant is useful when you have a value that should not change and you want to prevent accidental changes to it. Additionally, using constants can improve the performance of your code, since the Go compiler can make optimizations based on the assumption that the value of the constant will not change at runtime.
gistlibby LogSnag