Golang const keyword
last modified May 7, 2025
This tutorial explains how to use the const
keyword in Go. We'll
cover constant basics with practical examples of declaring and using constants.
The const keyword declares immutable values that cannot be modified during program execution. Constants provide compile-time fixed values.
In Go, const
can declare numeric, string, boolean, and other basic
types. Constants must be initialized at declaration and help make code more
readable and maintainable.
Basic constant declaration
The simplest use of const
declares a single immutable value. This
example shows basic constant syntax.
package main import "fmt" func main() { const pi = 3.14159 const appName = "MyGoApp" fmt.Println("Pi value:", pi) fmt.Println("Application:", appName) // pi = 3.14 // This would cause compile error }
pi
and appName
are constants that cannot be modified.
Attempting to change them would result in a compilation error.
Typed constants
Constants can have explicit types for better type safety. This example shows typed constant declarations.
package main import "fmt" func main() { const maxUsers int = 100 const defaultTimeout float64 = 30.5 const welcomeMessage string = "Hello, Gopher!" fmt.Printf("Max users: %d (%T)\n", maxUsers, maxUsers) fmt.Printf("Timeout: %f (%T)\n", defaultTimeout, defaultTimeout) fmt.Printf("Message: %s (%T)\n", welcomeMessage, welcomeMessage) }
Each constant has an explicit type declared. The %T
verb in
fmt.Printf
shows the type of each constant.
Multiple constants declaration
Go allows declaring multiple constants in a single block. This example shows the grouped constant syntax.
package main import "fmt" func main() { const ( daysInWeek = 7 hoursInDay = 24 minutesInHour = 60 ) fmt.Printf("Week has %d days\n", daysInWeek) fmt.Printf("Day has %d hours\n", hoursInDay) fmt.Printf("Hour has %d minutes\n", minutesInHour) }
The constants are grouped within parentheses. This syntax is cleaner for related constants and provides better code organization.
Constant expressions
Constants can be initialized with expressions evaluated at compile time. This example demonstrates constant expressions.
package main import "fmt" func main() { const ( secondsInMinute = 60 secondsInHour = secondsInMinute * 60 secondsInDay = secondsInHour * 24 ) fmt.Printf("Minute: %d seconds\n", secondsInMinute) fmt.Printf("Hour: %d seconds\n", secondsInHour) fmt.Printf("Day: %d seconds\n", secondsInDay) }
The constants use arithmetic expressions. These are evaluated at compile time, not runtime, making them efficient.
Iota for enumerated constants
Go's iota
identifier creates enumerated constants. This example
shows how to use iota effectively.
package main import "fmt" func main() { const ( Monday = iota + 1 Tuesday Wednesday Thursday Friday Saturday Sunday ) fmt.Println("Days:", Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) const ( Read = 1 << iota Write Execute ) fmt.Printf("Permissions: Read=%b, Write=%b, Execute=%b\n", Read, Write, Execute) }
iota
starts at 0 and increments by 1 for each constant. The second
group shows bitmask pattern creation using bit shifting.
String constants
String constants are common in Go programs. This example demonstrates their declaration and usage.
package main import "fmt" const ( greeting = "Hello, World!" farewell = "Goodbye for now!" errorMessage = "An error occurred" ) func main() { fmt.Println(greeting) fmt.Println(farewell) fmt.Println(errorMessage) const multiline = `This is a multiline string constant in Go` fmt.Println(multiline) }
String constants can be single-line or multiline (using backticks). They're often used for messages, templates, and configuration values.
Practical example: Configuration constants
This practical example shows how constants can organize configuration values. Constants help maintain consistency across the application.
package main import "fmt" const ( AppName = "InventoryManager" AppVersion = "1.2.3" MaxConnections = 100 TimeoutSeconds = 30 AdminEmail = "admin@example.com" SupportURL = "https://support.example.com" ) func main() { fmt.Printf("%s v%s\n", AppName, AppVersion) fmt.Println("Settings:") fmt.Printf("Max connections: %d\n", MaxConnections) fmt.Printf("Timeout: %d seconds\n", TimeoutSeconds) fmt.Printf("Contact: %s\n", AdminEmail) fmt.Printf("Support: %s\n", SupportURL) }
Application configuration is centralized in constants. This makes maintenance easier and prevents magic values in code.
Source
This tutorial covered the const
keyword in Go with practical
examples of declaring and using constants in various scenarios.
Author
List all Golang tutorials.