Identifiers and Keywords
- Identifiers
Identifiers are words with special meanings defined by programmers, such as variable names, constant names, and function names. In Go, identifiers can only consist of
letters,digits, and_, and they must begin with a letter or_, such asabc,_hello,_,_123, anda123.
- Keywords
Go has 25 keywords:
| |
In addition, Go has 37 reserved words:
| |
Variables in Go
- Origin of Variables
Data generated while a program is running is stored in memory. To manipulate a piece of data in code, we need to locate it in memory. However, directly accessing variables through memory addresses would make the code difficult to read and prone to errors. Therefore, we use variables to store the memory addresses of data, allowing us to locate the corresponding data in memory through those variables.
- Variable Types
Variables are used to store data. Different variables may store different types of data. After more than half a century of development, programming languages have largely established a standard set of types. Common variable data types include integers, floating-point numbers, and booleans. Every variable in Go has its own type and must be declared before it can be used.
Variable Declarations
Variables in Go must be declared before use. Duplicate declarations are not allowed within the same scope. In addition, variables declared in Go must be used.
Standard Declaration
The syntax for declaring a variable in Go is:
| |
Summary: A variable declaration begins with the var keyword, the variable type follows the variable name, and no semicolon is required at the end of the line.
| |
Grouped Declarations
Writing the var keyword for every variable declaration is cumbersome. Go supports grouped variable declarations, as shown below:
| |
Variable Initialization
When a variable is declared in Go, its corresponding memory region is automatically initialized. Each variable is initialized with the zero value of its type. For example:
The default value for integer and floating-point variables is 0. The default value for string variables is an empty string "". The default value for boolean variables is false. The default value for slices, functions, and pointer variables is nil.
We can also specify an initial value when declaring a variable. The syntax is as follows:
| |
For example:
| |
Go also supports declaring multiple variables on a single line:
| |
Note that no variable types are specified here. This is because the Go compiler infers the variable types from the types of the values on the right-hand side of the equals sign.
Type Inference
When a variable is declared and assigned a value at the same time, its type can be omitted. Go automatically determines the type of the variable on the left-hand side based on the value on the right-hand side. For example:
| |
Short Variable Declaration :=
Inside a Go function, you can use := to declare and assign variables. For example:
| |
Note that it cannot be used to declare global variables.
Anonymous Variables
When using multiple assignment, you can use an anonymous variable to ignore a value. An anonymous variable is represented by an underscore, _. For example:
| |
In Go, a function can return multiple values. When receiving the values returned by a function, you can use
_to ignore any value that is not needed.
Constants
Unlike variables, constants are immutable values. They are commonly used to define values that do not change while a program is running. Constant declarations are very similar to variable declarations, except that var is replaced with const. Constants must be assigned a value when they are defined.
| |
After the constants pi and e are declared, their values cannot change throughout the entire execution of the program.
Multiple constants can also be declared together:
| |
When multiple constants are declared together with const, omitting a value means that the value from the preceding line is reused. For example:
| |
In the example above, the constants n1, n2, and n3 all have the value 100.
iota
iota is Go’s constant counter and can only be used in constant expressions.
iota is reset to 0 whenever the const keyword appears. Each new line of constant declarations within const increments the iota counter once (iota can be understood as the line index within a const block). Using iota simplifies definitions and is particularly useful when defining enumerations.
For example:
| |
Several Common iota Examples:
- Using
_to skip certain values
| |
- Inserting a value in the middle of an
iotadeclaration
| |
- Defining units of magnitude
Here,
<<represents the left-shift operation.1<<10shifts the binary representation of 1 to the left by 10 bits, changing it from 1 to 10000000000, which is 1024 in decimal. Similarly,2<<2shifts the binary representation of 2 to the left by 2 bits, changing it from 10 to 1000, which is 8 in decimal.
| |
Defining multiple iota values on a single line:
| |