Go Constants
Go - Constants
Section titled “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.
Declaring Constants
Section titled “Declaring Constants”Constants can be declared individually or in a group.
const Pi = 3.14159 // Untyped floating-point constantconst Greeting = "Hello, Go!" // Untyped string constant
// Grouped constantsconst ( StatusOK = 200 // Untyped integer constant Version = "1.0.0" // Untyped string constant DebugMode = true // Untyped boolean constant)
// Typed constantsconst MaxUsers int = 1000Typed vs. Untyped Constants
Section titled “Typed vs. Untyped Constants”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 kindvar myInt64 int64 = UntypedNumber // OK, fits in int64// var myInt32 int32 = UntypedNumber // Compile error if UntypedNumber is too large for int32
const UntypedFloat = 3.0var f32 float32 = UntypedFloat // OKvar f64 float64 = UntypedFloat // OKNumeric Literals for Constants
Section titled “Numeric Literals for Constants”- Integer literals: Can be decimal (e.g.,
123), octal (prefix0oor0O, e.g.,0o177), hexadecimal (prefix0xor0X, e.g.,0xFF), or binary (prefix0bor0B, 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 and Escape Sequences
Section titled “String Literals and Escape Sequences”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\uXXXXor\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 Sequence | Meaning |
|---|---|
\a | Alert or bell (U+0007) |
\b | Backspace (U+0008) |
\f | Form feed (U+000C) |
\n | Newline (U+000A) |
\r | Carriage return (U+000D) |
\t | Horizontal tab (U+0009) |
\v | Vertical tab (U+000B) |
\\ | Backslash (U+005C) |
\' | Single quote (U+0027) - valid in rune literals |
\" | Double quote (U+0022) - valid in interpreted string literals |
\ooo | Octal character value (e.g., \141 is ‘a’) |
\xhh | Hexadecimal character value (e.g., \x61 is ‘a’) |
The iota Constant Generator
Section titled “The iota Constant Generator”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.