Constants in Go

In Go, constants are used to store fixed values that cannot be changed during the execution of a program. They are typically used for values that remain constant throughout the program, such as mathematical constants, configurations, or labels for specific states.
Introduction
A constant is a value that is defined once and cannot be modified by the program. Constants in Go can only be of the following types:
Boolean
Numeric (integer, float, or complex)
String
Explicit and Implicit Typing
In Go, constants are declared using the const keyword. Here’s the basic syntax:
const identifier [type] = value
identifieris the name of the constant.typeis optional and can be omitted if the type is implicitly clear.
Example without type:
const PI = 3.14159
In this case, the type is automatically inferred as float64.
Example with type:
const GREETING string = "hello"
It's also common to use uppercase names for constants to improve readability:
const INCHTOCM = 2.54
Typed and Untyped Constants
Go distinguishes between typed and untyped constants:
Typed constants: Declared with an explicit type.
Untyped constants: Type is inferred from the value and becomes fixed when used in a context that requires a specific type.
For example:
var n int
f(n + 5) // "5" becomes an int here because n is int
Compilation
Constants are evaluated at compile time, which means their values must be known before the program runs.
Valid example:
const C1 = 2 / 3 // OK, evaluated at compile time
Invalid example:
const C2 = getNumber() // Error: getNumber() cannot be evaluated at compile time
In the second example, getNumber() is a function that runs at runtime, so it cannot be used to assign a constant value.
Overflow
Go allows numeric constants to have arbitrary precision, meaning they won’t overflow.
Examples:
const Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009
const Log2E = 1 / Ln2
const BILLION = 1e9 // float constant
const HARD_EIGHT = (1 << 100) >> 97
Here, Ln2 is declared across multiple lines using the backslash \, which allows for long constant declarations.
Multiple Assignments
Go allows declaring multiple constants in a single line. For example:
const BEEF, TWO, C = "meat", 2, "veg"
You can also declare multiple constants of the same type in one go:
const MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY int = 1, 2, 3, 4, 5, 6
Here, all constants are explicitly typed as integers.
Enumerations
Constants can also be used to create enumerations, where a set of related values are grouped.
Example:
const (
UNKNOWN = 0
FEMALE = 1
MALE = 2
)
The iota keyword can simplify this:
const (
UNKNOWN = iota
FEMALE
MALE
)
The first iota is 0, and each subsequent constant gets the next integer value. This can also be typed:
type Gender int
const (
UNKNOWN Gender = iota
FEMALE
MALE
)
Here, UNKNOWN, FEMALE, and MALE are constants of type Gender.
Full Code Example
Here’s a complete file with all the examples covered:
package main
import "fmt"
func main() {
// Implicitly typed constants
const PI = 3.14159
const GREETING = "hello"
fmt.Println(PI) // 3.14159
fmt.Println(GREETING) // hello
// Explicitly typed constants
const INCHTOCM float64 = 2.54
const GREETING_EXPLICIT string = "hi"
fmt.Println(INCHTOCM) // 2.54
fmt.Println(GREETING_EXPLICIT) // hi
// Typed and untyped constants
var n int
fmt.Println(n + 5) // 5 becomes int
// Multiple assignments
const BEEF, TWO, C = "meat", 2, "veg"
fmt.Println(BEEF, TWO, C) // meat 2 veg
const MONDAY, TUESDAY, WEDNESDAY int = 1, 2, 3
fmt.Println(MONDAY, TUESDAY, WEDNESDAY) // 1 2 3
// Enumeration with iota
const (
UNKNOWN = iota
FEMALE
MALE
)
fmt.Println(UNKNOWN, FEMALE, MALE) // 0 1 2
// Typed enumeration with iota
type Gender int
const (
UNKNOWN_GENDER Gender = iota
FEMALE_GENDER
MALE_GENDER
)
fmt.Println(UNKNOWN_GENDER, FEMALE_GENDER, MALE_GENDER) // 0 1 2
// Overflow example
const Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009
fmt.Println(Ln2)
}
here is the link to try - https://go.dev/play/p/Cz7BVYv_iXM
This covers everything you need to know about constants in Go! In the next lesson, we'll dive into variables.




