variable
What are variables? A variable is a memory area that can store data at any time. When we apply for this memory area (declare variables), we need to specify the variable name and the data type of the variable. The data type is used to explain what value the variable can store.
Go is a strongly typed language, so Go variables need to be declared first and then used, and their data type cannot be changed after declaration.
Variable name
- The variable name must begin with a letter or an underscore.
- Variable names are case sensitive, e.g.
Version
andversion
are two different variables. - The variable cannot be named with keywords (25) and reserved words (37).
statement
There are two ways to declare Go variables, one is a standard variable declaration and the other is a short variable declaration.
Standard way to declare variables
The standard way Go declares a variable is to use keywordsvar
, Also note that the data type of Go variable is placed after the variable name, which is different from other languages:
Declare a variable:
var i int i = 10
Assign values to variables when declaring them
var i int = 10
When declaring that no data type is specified, Go infers its data type by assignment:
var i = 10
Declare multiple variables of the same type simultaneously
var m,n int //Declare two variables of the same type on the same line
When multiple variables are declared at the same time, you can also assign initial values:
var q,p = "test",10
Bulk declaration variables:
var( s string = "test" r int t,f bool )
How to declare variables briefly
In addition to declaring variables with the keyword var, Go also supports short local variable declarations.
A short way to use variables:=
, and there is no need to specify a data type, Go will infer the data language based on the value assigned when the declaration is:
Note that Go's local variables must be used after declaration, otherwise an error will be reported.
package main i := 10 //mistake func main(){ i := 10//Infer that i is an integer based on the assignment i = 100//Reassign the integer value i = "1" //Error, string can no longer be assigned}
Variable scope
Go is through packages (package
) organizes the code, and variables declared in the package (or functions, constants, structures, etc.) are distinguished from whether the first letter of the variable name is capitalized or not. When the first letter is capitalized, other packages can access the variable.
Define two variables in a package:
package A var age int = 10 //Not visible outside the package var Username string = "test" //Visible outside the package
Next, we introduce the above package in another package:
package B import A import "fmt" func main(){ ()//Available for access () //Error, Unable to access variables starting with lowercase letters}
constant
The value of a variable can be changed at any time during operation, while the value of a constant cannot be changed after assignment. The value of a constant is determined at the time of program compilation, so the constant must be assigned when declared.
Go language usageconst
The key is to declare a constant.
const Version = "1.0" const pi float = 3.14
Declare multiple constants like variables:
const ( v1 = 100 v2 v3 )
The above declaration method is constantv2
andv3
The value of 100.
iota constant generator
Sometimes we often need to declare some regular constants, such as enum values such as month or week:
const ( Sunday int = 0 Monday int = 1 Tuesday int = 2 Wednesday int = 3 Thursday int = 4 Friday int = 5 Saturday int = 6 )
The above method of declaring constants is more troublesome, and each constant value needs to be assigned.
And using Goiota
Constant generators can avoid the trouble of initializing one by one when declaring such regular constants:
const ( Sunday int = iota Monday Tuesday Wednesday Thursday Friday Saturday )
In this example,iota
The first line constantSunday
Will be set to 0, and the following constants will be incremented in turn.
use_
After the symbol, you can also skip a certain value or insert other values in the middle:
const ( s1 = iota //0 22 //1 _ //jump over s4 //3 s5 //4 s6 = 100 //Insert a value in the middle s7 = iota //0 )
iota
It can also be calculated and assigned to constants:
const ( January Month = 1 + iota February March April May June July August September October November December )
summary
The declaration of Go variables and constants puts the data type after the variable name, which is different from other programming languages. In addition, the case of variables and constant names determines whether the variable or constant is visible outside the package.
This is the article about the statement and use of variables and constants for re-learning Go language. This is all about this article. For more related Go language variables, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!