Declaring Constants
The declaration of constants is similar to that of variables. The keyword var
is replaced with the keyword const
.
Note: Constants must be assigned a value when declared.
In Go, all variables must be used after they are defined, but constants are an exception. Even if they are not used, there will be no error.
The syntax for declaring a constant is as follows:
const name [type] = value
name
is the name of the constant being declared. type
is the data type of the constant, but it can be omitted because Go can infer it automatically. The value at the end is the value assigned to the constant.
Note that only the following types can be used for constant declarations:
- Integer types
- Floating-point types
- Complex types
- Boolean types
- String types
Let's look at an example. Create a new file called const.go
in the ~/project
directory:
touch ~/project/const.go
Enter the following code in const.go
:
package main
import "fmt"
// Declare a single constant
const labex string = "LabEx" // explicitly specify the type as string
const labs = "LABS" // automatically inferred as string by the compiler
// Declare multiple constants
const (
hangzhou, chengdu = "HANGZHOU", "CHENGDU"
monday, tuesday, wednesday = "MONDAY", "TUESDAY", "WEDNESDAY"
)
func main() {
fmt.Printf("The type of labex is: %T, and its value is %s\n", labex, labex)
fmt.Printf("The type of labs is: %T, and its value is %s\n", labs, labs)
fmt.Println()
fmt.Println(hangzhou, chengdu)
fmt.Println(monday, tuesday, wednesday)
}
go run ~/project/const.go
After running the program, the output is as follows:
The type of labex is: string, and its value is LabEx
The type of labs is: string, and its value is LABS
HANGZHOU CHENGDU
MONDAY TUESDAY WEDNESDAY
Here, we demonstrate the declaration of single and multiple constants. When declaring multiple constants, it is recommended to use parentheses.
In the program, we only demonstrate the declaration of string constants. However, constants can also have other types, such as integers and booleans.
When declaring multiple constants using parentheses, if a constant is not initialized, it will inherit the previous constant's value.
Enter the following code in const.go
:
package main
import "fmt"
const (
monday = "MONDAY"
tuesday = "TUESDAY"
wednesday = "WEDNESDAY"
thursday
friday
)
func main() {
fmt.Println(monday, tuesday, wednesday, thursday, friday)
}
go run ~/project/const.go
The output is as follows:
MONDAY TUESDAY WEDNESDAY WEDNESDAY WEDNESDAY
Here, thursday
and friday
do not have explicit values. According to Go's rules, when a constant is not explicitly initialized, it inherits the value of the previous constant. In this case:
thursday
inherits the value of wednesday
.
friday
also inherits the value of wednesday
.
This behavior allows for concise code but can lead to unexpected results if you are not aware of it.
If you want thursday
and friday
to have distinct values, you need to explicitly assign them:
const (
monday = "MONDAY"
tuesday = "TUESDAY"
wednesday = "WEDNESDAY"
thursday = "THURSDAY"
friday = "FRIDAY"
)
This will produce the expected output:
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY