Constants

Contents
const block
iota enumerations
Typed vs untyped
When constants win

const block

The const keyword declares values that never change, and a const block groups related values in one place.

The detailed guide lives in the article about constants, while the pattern below covers everyday use.

package main import "fmt" const ( appName = "course" maxRetries = 3 pi = 3.14159 ) func main() { fmt.Println(appName, maxRetries, pi) }

go run .

course 3 3.14159

iota enumerations

The iota identifier counts lines inside a const block starting from zero, which is ideal for enumerations such as days or statuses.

Repeated lines inherit the expression above, so only the first line needs the explicit iota.

package main import "fmt" const ( sun = iota mon tue ) func main() { fmt.Println(sun, mon, tue) }

go run .

0 1 2

Typed vs untyped

A constant with an explicit type works only with that type, while an untyped constant adapts to int or float64 or string as the context requires.

Prefer untyped constants for math and configuration defaults, and add a type when the value must stay fixed to one representation.

package main import "fmt" const ratio = 0.5 const limit int = 10 func main() { fmt.Println(ratio + 1) fmt.Println(ratio + 0.25) fmt.Println(limit + 5) }

go run .

1.5
0.75
15

When constants win

Choose constants for values that are known before compilation and must never change during the run: mathematical factors, limits, status codes, and configuration defaults.

Constants document intent, let the compiler check misuse early, and remove a whole class of accidental reassignment bugs.

Next: Types

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: