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.
[Literals in go`const FN = fun () { fmt.Println("Hello World") }
### 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 `DISCOUNT`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(
}
$$
would become
$$
func main () {
fmt.Println(
}
$$
### 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: