Go Reference ยท Keywords and Statements
The const declaration
Reference for Go const: typed and untyped constants, arbitrary-precision constant arithmetic, allowed types, grouped declarations and compile-time errors.
const declares names whose values are known at compile time and can never change. Constants can be numbers, strings, booleans and runes. Unlike variables, they are not stored in memory at a particular address; the compiler substitutes their values.
The most important idea is the untyped constant: const big = 1 << 100 has no type yet and is computed with arbitrary precision, so it can be used wherever the value fits (float64(big)) but not where it does not (int(big) is a compile error). Typed constants have a fixed type and obey that type's rules.
const Pi = 3.14159 const Timeout time.Duration = 5 * time.Second
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
const Name = value const Name Type = value const ( Name1 = value1 Name2 )
| Part | Description |
|---|---|
Name |
One or more identifiers. |
Type |
Optional. Must be a boolean, numeric or string type (including named types with such an underlying type). |
value |
A constant expression: literals, other constants, operators, and a few built-ins such as len of a constant string or array, unsafe.Sizeof, real, imag, complex, min and max on constants. |
Errors and edge cases:
- Function calls, variables, slices, maps and structs are not constant expressions:
const x = f()fails withis not constant. cannot use ... (untyped int constant 300) as int8 value (overflows): an untyped constant that does not fit the target type is rejected at compile time.- Constant division by zero is a compile error, while the same division with variables panics at run time.
- In a group, a line with no expression repeats the previous expression and type; this is what makes
iotaenumerations work.
Usage notes
- Default types: untyped integer, floating-point, rune, string and boolean constants become
int,float64,rune,stringandboolwhen a type is needed and none is given (x := 5). - Kinds: an untyped constant has a kind (integer, rune, floating-point, complex). Mixed expressions use the "later" kind:
1 + 2.0is an untyped floating-point constant. - Typed constants are strict:
const n int = 5cannot be added to afloat64without conversion; an untyped5can. - Precision: compilers must represent integer constants with at least 256 bits and floats with at least 256-bit mantissas; the compiler rejects values it cannot represent.
- Not addressable: you cannot take
&Pi. Copy it into a variable if you need a pointer. - Enumerations are built with typed constants and
iota. - Strings are constant too, and indexing a constant string yields a non-constant byte.
Related
iota,var,type- Lessons: Constants, Basic types, Custom types