What a constant is
A constant is a named value that cannot change after it is declared. You make one with the const keyword:
const pi = 3.14
fmt.Println(pi) // 3.14
Use a constant for a value that should stay the same for the whole run of the program: a mathematical constant, a number of seconds in an hour, a fixed limit. Giving it a name documents what the number means, and the compiler guarantees that no later line can change it by accident.
Constants cannot be reassigned
If you try to give a constant a new value, the program does not compile:
const pi = 3.14
pi = 5
fmt.Println(pi)
cannot assign to pi (neither addressable nor a map index expression)
Because the error appears at compile time, nothing is printed, not even the original value. The same applies to pi++ and to taking the address of a constant with &pi.
How const differs from var
- It is declared with
const, notvaror:=. The short form:=only creates variables. - The value must be known when the program compiles. A literal or an expression built from constants works, such as
const seconds = 60 * 60. The result of calling a function does not:const n = f()is rejected withf() (value of type int) is not constant. - Only simple types are allowed. Constants are numbers, strings and booleans. Slices and maps can't be constants.
- An unused constant is fine. Go requires every local variable to be used, but not every constant.
Several constants can be grouped in one block:
const (
a = 1
b = "x"
)
Untyped constants
In const pi = 3.14 no type is written, so pi is an untyped constant: it is just the number 3.14 with no fixed type yet. It adapts to the place where you use it. That is why pi * float64(r*r) works with a float variable. If you want a fixed type, write it after the name, as in const rate float64 = 2. When an untyped constant is stored in a variable by :=, the variable gets the default type for that kind of constant, which is float64 for 3.14.
The distinction has a consequence. A typed constant behaves like a typed variable: const rate float64 = 2 can't be multiplied by an int variable without a conversion. An untyped const rate = 2 can be used with either an int or a float64.
Constants in practice
Real programs use constants to avoid "magic numbers". Writing const secondsPerHour = 60 * 60 once, and using the name in every calculation, tells the reader what 3600 means and gives one place to edit if the value ever has to change. The standard library also exports constants of its own: math.Pi holds pi to many more digits than 3.14, and printing it gives 3.141592653589793.
When you hesitate between const and a variable, ask whether the value is known before the program runs and never changes. If so, make it a constant.
The exercise, step by step
The starter code declares pi as a constant, then tries to change it, then prints it. The goal is to print 3.14 without an error.
- Find the cause. The line
pi = 5assigns to a constant, which Go forbids. - Delete that line. Don't try to change
consttovar, because the lesson is about keeping the value fixed. - Keep the rest. The declaration
const pi = 3.14and thefmt.Println(pi)stay exactly as they are.
const pi = 3.14
fmt.Println(pi)
The blank lines where the assignment used to be do no harm. The program prints 3.14.
Key takeaways
constdeclares a named value that can't change.- Reassigning a constant is a compile-time error.
- A constant's value must be known at compile time, so function results can't be used.
- Constants without a written type are untyped and adapt to their use.
- If a value must change while the program runs, make it a variable instead.