đź’¬ Debugging & Tutorials

Go - constant variables and const keyword

A

Anthony Olajide

Jan 2, 2026 at 2:56 PM

• 1 reply • 394 views
Programming languages have the notion of constants which means “variables that can not be mutated once declared and initialized”.

Go also has almost the same meaning, but in a different context. To initialize a variable as a constant with a value of 10, we can do something like this:

$$
const DISCOUNT = 10
$$

In Go, constants mean “storing a literal to a variable”, this can be seen as a version of pattern matching in Erlang.

Image

[s][[/s]Literals in go](https://www.phind.com/search?cache=n05row4wk42ocrf4520s6vzm) are constructs that can create an instance of strings, numbers, booleans, composite structures, functions, or expressions. Hence unlike many programming languages, constants in go can be [s]`[/s]const FN = fun () { fmt.Println("Hello World") }`.

[s]### [/s]An explanation
Initializing a variable as a constant means the variable holds the value and possibly the type of the literal it is assigned to. Anywhere the [s]`[/s]DISCOUNT` above is used, the compiler assumes that as writing the integer literal [s]`[/s]10`.

At compile time, the compiler swaps all representations of a constant value with the value it was assigned to. Such that

$$
func main () {
fmt.Println("%v", DISCOUNT)
}
$$

would become

$$
func main () {
fmt.Println("%v", 10)
}
$$

[s]### [/s]Big difference
In some programming languages, Constants mean the value of a variable once declared and initialized can never change

In contrast to what we have understood – constants are used to enforce that a variable is immutable, in Go, constants can be used to assign names to a literal exactly once, while at compile time, the name is swapped with the value of the constant - ensuring cleaner code, the idiomatic go.

Shalom…

Source: https://limistah.dev/posts/go-const/

1 Reply

Sign in to join the conversation

d

diltony@yahoo.com

6 days ago
This is pretty interesting, go is one of my favourite languages too.