TExcelTAD
Home › Go › Go Constants

Go Constants

A constant is a name for a value that never changes. Constants are declared with the const keyword and are evaluated at compile time, so their value must be known before the program runs — numbers, strings, booleans, or expressions built from them.

go · try it yourself
package main

import "fmt"

const Pi = 3.14159
const AppName = "Tracker"

func main() {
	const maxUsers = 50
	radius := 2.0

	fmt.Println(AppName, "allows", maxUsers, "users")
	fmt.Println("Circle area:", Pi*radius*radius)
}
Tracker allows 50 users
Circle area: 12.56636

Trying to change a constant is a compile error:

go · try it yourself
package main

const limit = 10

func main() {
	limit = 20
}
# command-line-arguments
./example.go:6:2: cannot assign to limit (neither addressable nor a map index expression)

Typed and untyped constants

A constant declared without a type, like const Pi = 3.14159, is untyped. It has a default type (here float64), but it can be used wherever a compatible type is expected. This makes constants very flexible:

go · try it yourself
package main

import "fmt"

const big = 1000          // untyped integer constant
const typed int32 = 1000 // typed constant

func main() {
	var a int64 = big    // ok: untyped constant fits int64
	var b float64 = big  // ok: also fits float64
	var c int32 = typed  // ok: same type

	fmt.Println(a, b, c)
}
1000 1000 1000

If you tried var d int64 = typed, the compiler would complain, because a typed int32 constant cannot be assigned to an int64 variable without a conversion.

Untyped (const x = 5) Typed (const x int = 5)
Can mix with any numeric type yes no — only with its own type
Precision arbitrary while compiling limited to the type
Typical use most constants when the type itself matters

Constant blocks and iota

Related constants are usually grouped in a const ( ... ) block. Inside such a block, the special identifier iota starts at 0 and increases by one on each line. It is Go's way of creating enumerations.

go · try it yourself
package main

import "fmt"

const (
	Small  = iota // 0
	Medium        // 1  (repeats "= iota")
	Large         // 2
)

const (
	_  = iota             // skip 0
	KB = 1 << (10 * iota) // 1 << 10
	MB                    // 1 << 20
	GB                    // 1 << 30
)

func main() {
	fmt.Println(Small, Medium, Large)
	fmt.Println(KB, MB, GB)
}
0 1 2
1024 1048576 1073741824

When a line in a const block has no expression, it repeats the previous one — with the new iota value. That is why MB and GB work.

Naming constants

Go does not use ALL_CAPS for constants. They follow normal naming rules: maxRetries inside a package, MaxRetries if exported. The capital first letter only means "exported", not "constant".

Tip: Use constants for magic numbers and fixed strings — const taxRate = 0.2 reads better than a bare 0.2 scattered through your code, and you change it in one place.

Test yourself

What value does iota have on the first line of a const block?

  1. 1
  2. 0
  3. -1
  4. It is undefined

Which of these can be a constant?

  1. The result of a function call at run time
  2. A slice []int{1, 2}
  3. The string "admin"
  4. A map

Why can an untyped constant const n = 3 be assigned to both int64 and float64 variables?

  1. Go converts all numbers automatically
  2. Untyped constants take on the type required by the context
  3. Constants are always float64
  4. It cannot