Go Language Constants
A constant is an identifier for a simple value, a quantity that is not modified during program execution. The data types of constants can only be Boolean, numeric (integer, floating-point, and complex), and string.
Word meaning: const Is constantAn abbreviation of (constant), representing an immutable value in a program.
Basic syntax and parameters
The format for defining a constant:
const identifier [type] = value
Syntax Description
- identifier: constant name, following Go identifier naming rules (letters, digits, underscores, and cannot start with a digit).
- type(optional): the type name; if omitted, it is inferred by the compiler.
- value: the value of the constant.
Definition
- Explicit type definition:
const b string = "abc" - Implicit type definition:
const b = "abc" - Batch definition:
const c_name1, c_name2 = value1, value2
Example
Example 1: Basic Constant Definition
Example
package
main
import
"fmt"
func
main
()
{
const
LENGTH
int
=
10
const
WIDTH
int
=
5
var
area
int
const
a
,
b
,
c
=
1
,
false
,
"str"
// Multiple assignment
area
=
LENGTH
WIDTH
fmt
.
Printf
(
"Area is : %d"
,
area
)
println
()
println
(
a
,
b
,
c
)
}
Expected output:
面积为 : 50
1 false str
Code analysis:
const LENGTH int = 10Defines an integer constant LENGTH with a value of 10.const WIDTH int = 5Defines an integer constant WIDTH with a value of 5.const a, b, c = 1, false, "str"Uses multiple assignment to define multiple constants of different types at the same time.area = LENGTH * WIDTHCalculates the area and assigns it to the variable area.
Constant Groups and Enumeration
Go has no enum keyword; constant groups are usually used to achieve enumeration:
Example
package
main
import
"fmt"
// Define a gender enumeration
const
(
Unknown
=
0
Female
=
1
Male
=
2
)
func
main
()
{
fmt
.
Printf
(
"Unknown=%d, Female=%d, Male=%d\n"
,
Unknown
,
Female
,
Male
)
}
Expected output:
未知=0, 女性=1, 男性=2
Code analysis:
- Using a constant group, you can define a set of related constant values.
- The numbers 0, 1, and 2 represent unknown gender, female, and male respectively.
Application of Built-in Functions in Constants
Built-in functions such as len()、cap()、unsafe.Sizeof() can be used in constant expressions to compute values.
**Note:** Functions in constant expressions must be built-in functions.
Example
package
main
import
(
"fmt"
"unsafe"
)
const
(
a
=
"abc"
b
=
len
(
a
)
c
=
unsafe
.
Sizeof
(
a
)
)
func
main
()
{
fmt
.
Printf
(
"a=%s, b=%d, c=%d\n"
,
a
,
b
,
c
)
}
Expected output:
a=abc, b=3, c=16
Code analysis:
a = "abc": string constant.b = len(a): use len() to get the string length, resulting in 3.c = unsafe.Sizeof(a): use unsafe.Sizeof() to get the memory usage of a variable, resulting in 16 bytes (a string struct occupies 16 bytes on a 64-bit system).
The iota Constant Counter
iota is a special constant in Go, also called the constant counter. It can automatically generate increasing sequence values in a const declaration block.
Key Features:
iotainconstis reset to 0 when the keyword appears.- Each new line of constant declaration in const automatically increments iota by 1.
- It can be understood as the line index in a const block (starting from 0).
Example 1: Basic iota Usage
Example
package
main
import
"fmt"
// iota starts at 0 and increases by 1 each line
const
(
a
=
iota
// 0
b
// 1 (value omitted, using the previous line's iota by default)
c
// 2
)
func
main
()
{
fmt
.
Printf
(
"a=%d, b=%d, c=%d\n"
,
a
,
b
,
c
)
}
Expected output:
a=0, b=1, c=2
Code analysis:
- When a constant value is omitted, the previous line's iota value is used by default.
- Therefore b=1, c=2.
Example 2: Detailed iota Usage
Example
package
main
import
"fmt"
func
main
()
{
const
(
a
=
iota
// 0
b
// 1
c
// 2
d
=
"ha"
// Independent value, iota += 1
e
// "ha",iota += 1
f
=
100
// Independent value, iota += 1
g
// 100,iota += 1
h
=
iota
// 7, resume using the iota value
i
// 8
)
fmt
.
Printf
(
"a=%d, b=%d, c=%d, d=%s, e=%s, f=%d, g=%d, h=%d, i=%d\n"
,
a
,
b
,
c
,
d
,
e
,
f
,
g
,
h
,
i
)
}
Expected output:
a=0, b=1, c=2, d=ha, e=ha, f=100, g=100, h=7, i=8
Code analysis:
- When a value is explicitly assigned, that value is used, but iota still increments.
- When the assignment is omitted, the previous line's value is used.
- in
h = iota, the current value of iota (7) is used again.
Example 3: iota and Bit Shift Operations
Example
package
main
import
"fmt"
const
(
i
=
1
<< iota
// 1 << 0 = 1
j
=
3
<< iota
// 3 << 1 = 6
k
// 3 << 2 = 12
l
// 3 << 3 = 24
)
func
main
()
{
fmt
.
Printf
(
"i=%d, j=%d, k=%d, l=%d\n"
,
i
,
j
,
k
,
l
)
}
Expected output:
i=1, j=6, k=12, l=24
Code analysis:
<<is the left-shift operator,x << nis equivalent tox * 2^n。i = 1 << 0 = 1: 1 shifted left by 0 bits, resulting in 1.j = 3 << 1 = 6: 3 shifted left by 1 bit, binary 11 becomes 110, which is 6.k = 3 << 2 = 12: 3 shifted left by 2 bits, binary 11 becomes 1100, which is 12.l = 3 << 3 = 24: 3 shifted left by 3 bits, binary 11 becomes 11000, which is 24.
Additional Notes
1 << n = 1 * 2^n3 << n = 3 * 2^n