How to Declare Constants in Go with const
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
const <name> = <value>
const (
<name> = <value>
<name> = <value>
)
| Part | What it does |
|---|---|
const | Declares 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:

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
go run main.go
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:
package main
import "fmt"
const issuer = "https://login.example.org"
func main() {
issuer = "https://elsewhere.example.org"
fmt.Println(issuer)
}
go run main.go
# command-line-arguments
./main.go:8:2: cannot assign to issuer (neither addressable nor a map index expression)

What Happened
- The constants worked like values.
maxRetries * 2printed6andtimeout * 2printed5. - They had no type written, and didn't need one. Constants like these are untyped:
3behaves as anintand2.5as afloat64when 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
| Mistake | What happens | Do this instead |
|---|---|---|
Using := for a constant | := declares a variable, which can be changed | Use const |
| Calling a function in a constant | The value must be known when the program compiles, so const now = time.Now() doesn't compile | Use var for values computed at run time |
| Writing constants in capital letters by habit | In Go, a capital first letter makes a name visible to other packages | Choose the case for its visibility, not to mark a constant |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
cannot assign to issuer (neither addressable nor a map index expression) | Code assigns to a constant | Use a variable for a value that changes |
time.Now() (value of struct type time.Time) is not constant | A constant's value comes from a function call | Declare 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
- Constant declarations and Constants, in the Go specification.
