Skip to main content

How to Declare Constants in Go with const

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

In Go, const declares a value that never changes: const issuer = "https://login.example.org". Several constants can share one const ( ... ) block, and Go refuses to compile code that assigns to a constant.

Syntax​

Syntax
const <name> = <value>

const (
<name> = <value>
<name> = <value>
)
PartWhat it does
constDeclares one constant, or a block of them in parentheses.
<name>The constant's name. Like any Go name, it starts with a capital letter only when other packages may use it.
<value>A value the compiler can work out on its own, such as a number or a string.

The Go specification: "A constant declaration binds a list of identifiers (the names of the constants) to the values of a list of constant expressions."

The Example​

The example declares a string constant on its own and two number constants in a block, and prints them. Then a changed version of the program tries to assign a new value to one of them.

Prerequisites​

  • Go. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which declares const issuer on its own and maxRetries and timeout in a const block, then prints issuer, maxRetries times 2 and timeout times 2.

main.go
package main

import "fmt"

const issuer = "https://login.example.org"

const (
maxRetries = 3
timeout = 2.5
)

func main() {
fmt.Println(issuer)
fmt.Println(maxRetries * 2)
fmt.Println(timeout * 2)
}

Step 2: Run It​

Terminal
go run main.go
Output
https://login.example.org
6
5

The command is the same in zsh, bash and PowerShell.

Step 3: Try to Change a Constant​

Replace main.go with a version that assigns a new value to issuer:

main.go
package main

import "fmt"

const issuer = "https://login.example.org"

func main() {
issuer = "https://elsewhere.example.org"
fmt.Println(issuer)
}
Terminal
go run main.go
Output
# command-line-arguments
./main.go:8:2: cannot assign to issuer (neither addressable nor a map index expression)

A zsh terminal where go run main.go first prints the issuer, 6 and 5, then, for the changed file, fails with cannot assign to issuer (neither addressable nor a map index expression).

What Happened​

  • The constants worked like values. maxRetries * 2 printed 6 and timeout * 2 printed 5.
  • They had no type written, and didn't need one. Constants like these are untyped: 3 behaves as an int and 2.5 as a float64 when they're used, which the specification calls their "default type".
  • The assignment didn't compile. The specification says the left side of an assignment "must be addressable, a map index expression, or (for = assignments only) the blank identifier", and a constant is none of those, which is exactly what the error message says.

Common Mistakes​

MistakeWhat happensDo this instead
Using := for a constant:= declares a variable, which can be changedUse const
Calling a function in a constantThe value must be known when the program compiles, so const now = time.Now() doesn't compileUse var for values computed at run time
Writing constants in capital letters by habitIn Go, a capital first letter makes a name visible to other packagesChoose the case for its visibility, not to mark a constant

Troubleshooting​

SymptomLikely causeFix
cannot assign to issuer (neither addressable nor a map index expression)Code assigns to a constantUse a variable for a value that changes
time.Now() (value of struct type time.Time) is not constantA constant's value comes from a function callDeclare it with var

FAQ​

What's the difference between const and var?​

A const is fixed when the program compiles and can never change. A var holds a value the program can change, and its value can come from a function call.

Should Go constants be in capital letters?​

No. The case of the first letter decides whether other packages can use the name, so a constant used only in its own package starts with a small letter, like issuer here.

Conclusion​

const gives a name to a value the compiler knows and nobody can change. Group related constants in a const ( ... ) block.

Additional Resources​