Skip to content

Go Constants

Constants in Go are fixed values that cannot be changed during program execution. They are declared using the const keyword and can be of basic types like boolean, rune, integer, floating-point, complex, or string.

Unlike variables, constants must be initialized at the time of declaration. Their value is determined at compile time.

Constants can be declared individually or in a group.

const Pi = 3.14159 // Untyped floating-point constant
const Greeting = "Hello, Go!" // Untyped string constant
// Grouped constants
const (
StatusOK = 200 // Untyped integer constant
Version = "1.0.0" // Untyped string constant
DebugMode = true // Untyped boolean constant
)
// Typed constants
const MaxUsers int = 1000

Go constants can be typed or untyped.

  • Typed constants are explicitly given a type (e.g., const MyInt int = 10). They adhere strictly to the rules of that type.
  • Untyped constants have a “kind” (e.g., integer, float, string) but no specific fixed type until they are used in a context that requires one (like assignment to a variable or use in an expression with typed values). Untyped constants offer more flexibility as they can be assigned to variables of any compatible type without explicit conversion, provided the value fits. Numeric untyped constants support arbitrary precision arithmetic at compile time.
const UntypedNumber = 12345678901234567890 // High-precision integer kind
var myInt64 int64 = UntypedNumber // OK, fits in int64
// var myInt32 int32 = UntypedNumber // Compile error if UntypedNumber is too large for int32
const UntypedFloat = 3.0
var f32 float32 = UntypedFloat // OK
var f64 float64 = UntypedFloat // OK
  • Integer literals: Can be decimal (e.g., 123), octal (prefix 0o or 0O, e.g., 0o177), hexadecimal (prefix 0x or 0X, e.g., 0xFF), or binary (prefix 0b or 0B, e.g., 0b1010).
  • Floating-point literals: Can have an integer part, decimal point, fractional part, and an optional exponent (e.g., 3.14, 1.0e3, .25).
  • Rune literals: Represent a single Unicode character, enclosed in single quotes (e.g., 'a', '\n', '世'). They have an integer kind.
  • Imaginary literals: Form complex constants (e.g., 3i, 2.718i).
const (
DecimalConst = 42
OctalConst = 0o52 // 42 in decimal
HexConst = 0x2A // 42 in decimal
BinaryConst = 0b101010 // 42 in decimal
FloatConst = 2.71828
RuneConst = 'G'
ComplexConst = 1 + 2i
)

String literals are enclosed in double quotes "..." or backticks `...`.

  • Interpreted string literals (double quotes): Allow escape sequences like \n (newline), \t (tab), \" (double quote), \\ (backslash), and Unicode escapes like \uXXXX or \UXXXXXXXX.
  • Raw string literals (backticks): Do not process escape sequences and can span multiple lines. Carriage returns (\r) inside raw string literals are dropped.
const InterpretedString = "Hello\tWorld\nThis is a G\u00F6 program."
const RawString = `This is a raw string.
It can span multiple lines.
No \escapes are processed.`
// Example showing escape sequences
// package main
// import "fmt"
// func main() {
// fmt.Println(InterpretedString)
// fmt.Println(RawString)
// }

Common Escape Sequences in Go strings and rune literals:

Escape SequenceMeaning
\aAlert or bell (U+0007)
\bBackspace (U+0008)
\fForm feed (U+000C)
\nNewline (U+000A)
\rCarriage return (U+000D)
\tHorizontal tab (U+0009)
\vVertical tab (U+000B)
\\Backslash (U+005C)
\'Single quote (U+0027) - valid in rune literals
\"Double quote (U+0022) - valid in interpreted string literals
\oooOctal character value (e.g., \141 is ‘a’)
\xhhHexadecimal character value (e.g., \x61 is ‘a’)

iota is a special predeclared identifier used in const declarations to simplify the creation of sequential integer constants (enumerations). It starts at 0 for each const block and increments by 1 for each subsequent constant specification in the block.

const (
C0 = iota // C0 = 0
C1 = iota // C1 = 1
C2 = iota // C2 = 2
)
const (
FlagRead = 1 << iota // FlagRead = 1 (1 << 0)
FlagWrite // FlagWrite = 2 (1 << 1), iota increments, expression repeats
FlagExecute // FlagExecute = 4 (1 << 2)
_ // Skip a value (iota increments)
FlagAdmin // FlagAdmin = 16 (1 << 4)
)
// package main
// import "fmt"
// func main() {
// fmt.Println(C0, C1, C2) // Output: 0 1 2
// fmt.Println(FlagRead, FlagWrite, FlagExecute, FlagAdmin) // Output: 1 2 4 16
// }

Constants are a fundamental part of writing robust and maintainable Go code, often used for defining fixed parameters, status codes, or bitmasks.