Constants
Constants in Go are declared similar to variables but with const
keyword. Constant in Go can appear anywhere variable can appear and does not have shorthand as var has. The simplest way to define a string constant would be
Yes, I know what you may be thinking, should the constant name not be all capital? No, in Go, it's not mandatory, constant can be all capital, came cased or usual snake cased. There are no such specific guidelines on how constants should be named
But for the sake of consistency, I would suggest that one should use camel case while naming constants. But how do constants differ from variables in Go? In Go, like any other language constants can not be reassigned. To test this hypothesis, let's run the following code in Go Playground.
The above code, when executed would error. Because constants can not be reassigned
All set, right? :) No
In Go, a numeric constant has no type, until its given one. One need to force typecast const to have it its type, like following
In Go, a numeric constant has no type, until its given one.
Last updated