01 Introduction
In programming, the compiler must replace the name of the variable representing the data with the memory address where the data resides. The name, type, and memory address of the variable are usually fixed, but the data stored in the memory address may change during program execution.
The Golang language compiler needs to clarify the memory boundaries of the variable before using variables. By declaring the type used by the variable, the compiler can clarify the memory boundaries of the variable.
Because the Golang language is a static language, it cannot automatically determine the memory boundary of the variable by analyzing the value of the variable at runtime, so in the Golang language, variables need to be declared before using them.
In the Golang language, under the premise of explicit assignment of variables, declared variables can omit types, and when declaring methods using short variables, there is no need to specify variable types. At this time, the compiler will infer variable types based on the value of the variable through type, thereby clarifying the memory boundaries of the variable.
02 Variable declaration method
The Golang language variable declaration is relatively flexible. It supports a variety of variable declaration methods, including standard declaration variables, declare variables without explicit assignment of initial value, omitting type declaration variables and short variable declarations.
Standard Declaration Variables
var a int = 100
Reading the above code, we can find that the standard way of declaring variables in the Golang language contains 4 parts, namely the var keyword, variable name a, variable type int and variable value 100.
Declare variables without explicitly assigning initial values
If we do not want to assign initial values to declared variables, we can also not explicitly assign values to the variables, omitting the equal sign and value, as shown below:
var a int
If the variable is not explicitly assigned a value, the value of the variable is the zero value of the type, that is, the default value of the type.
Omit type declaration variables
The 4 parts of the standard declared variables can be omitted, in addition to not explicitly assigning values to variables, types can also be omitted, as shown below:
var a = 100
At the beginning of the article, we introduce that the compiler needs to determine the memory boundary of the variable based on the type of the variable. If the type of the variable is not specified when declaring the variable, can the compiler also determine the memory boundary of the variable?
The answer is yes. The reason is that the Golang compiler can infer the type of the variable based on the assignment of the variable.
Careful readers may ask, can the Golang language omit the type and explicit assignment initial values in the 4 parts of the standard variable declaration method?
The answer is no. The Golang compiler must at least clarify the type or initial value of the variable. If both are omitted, the compiler will not be able to obtain the type of the variable, so that the memory boundaries of the variable cannot be clarified based on the type.
Short variable declaration
The above two variable declaration methods simplified based on the standard variable declaration method require the use of the keyword var. Readers may ask whether the keyword var can be omitted?
The answer is yes. The Golang language also has the most simplified way to declare variables, short variable declarations, using short variable declarations, and can omit the keyword var and the type of variables at the same time.
a := 100
Read the above code, it is a short variable declaration. Compared with the standard variable declaration method, this method omits the keyword var and the type of the variable. However, careful readers may have found that = is changed to :=. The Golang compiler can make type inferences based on the assignment of variables to obtain the type of variables, thereby clarifying the memory boundaries of variables.
The short variable declaration method is the most simplified variable declaration in the Golang language, but it also has limitations, and it can only be used for local variable declarations.
Explicit type conversion
Whether it is omitting the type explicit assignment of the initial value to declare the variable or short variable declaration, they are all the default types of the variables that the Golang compiler uses type inference to obtain the variable's default type based on the assignment of the variable.
If we also don't want to use the default type of the variable, then we can get the variable type we want by explicit type conversion, as shown below:
var a = int8(100) b := int8(60)
Variable list declaration
All ways to declare variables are supported for variable list declarations. The so-called variable list declaration is to use the var keyword to declare multiple variables through a line of code.
var a, b, c int = 100, 200, 300 var d, e, f int var g, h, i = 400, "Hello", true j, k, l := 500, "world", 3.14
Variable declaration block
The Golang language also supports variable declaration blocks, using the keyword var to put multiple single variable declarations or variable list declarations together, as shown below:
var ( a int = 100 b int = 200 ) var ( c = 300 d = 3.14 f = true ) var ( e, f, g int = 10, 20, 30 h, i, j string = "a", "b", "c" )
03Usage scenarios
We have introduced a variety of ways to declare variables. So what are the usage scenarios of each Golang language variable declaration method? First of all, we need to understand the scope of variables in the Golang language, namely package-level variables, global variables and local variables.
Packet-level variables
The scope of package-level variables is that they can only be used in the same package.
Package-level variables can only be used to declare variables with the var keyword. If the variable is explicitly initialized, the method of omitting the type to declare variables is generally used.
Global variables
The scope of global variables is available anywhere in the project.
If the variable name of a package-level variable is capitalized, that is, the exported variable in the Golang language, then this variable becomes a global variable and can be used globally.
Global variables can only be used to declare variables with the var keyword. If the variable is explicitly initialized, the method of omitting the type to declare variables is generally used.
Local variables
The scope of local variables is that they can only be used in function bodies or method bodies.
For local variables, we generally prefer to use short variable declaration methods. Unless we do not want to explicitly initialize local variables, we can use the method of omitting the type declaration of variables.
04 Notes:
Each statement outside the function must start with a keyword (var, const, func, etc.);
:= cannot be used outside the function;
_ is mostly used for placeholding, indicating that the value is ignored;
05 Summary
In this article, we introduce why the Golang language needs to declare variables before using variables; what are the ways of declaring variables in Golang language? What are the ways of declaring variables in Golang language? What scenarios does each variable declaration method of Golang language apply to?
This is the article about the various variable declaration methods and usage scenarios of Golang language. For more information about Golang's multiple variable declarations and usage scenarios, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!