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

const str = "Hey, I am defined as constant"

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.

package main
import "fmt"
const Pi = 3.14
func main() {
    const World = "世界"
    fmt.Println("Hello", World)
    fmt.Println("Happy", Pi, "Day")
    World = "World"
}

The above code, when executed would error. Because constants can not be reassigned

./prog.go:8:8: cannot assign to World

Go build failed.

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

const Big = 1 << 60
fmt.Println(int64(Big))
// => 1152921504606846976

In Go, a numeric constant has no type, until its given one.

Last updated