For friends who only have Python experience, they may not understand the word declaration very well. They can be used directly in Python without declaring types or other things.
Go is a statically typed language. Since the compiler checks the type of variables during compilation, it is required that all variables have clear types.
Variables need to be declared before use. Declaring a type is agreed that you can only assign values of this type as a variable.
There are generally four methods for declarations, among which the first two can also be used to define constants, just turn the keyword var into const.
The first type: declare a variable on one line
var <name> <type>
where var is the keyword (fixed), name is the variable name, and type is the type.
Use var , although only the type is specified, Go will implicitly initialize it, for example, the string type is initialized as an empty string, the int type is initialized as 0, the float is initialized as 0.0, the bool type is initialized as false, and the pointer type is initialized as nil.
If you want to initialize it in the declaration process, you can write it like this
var name string = "Python programming"
The complete code in the Go file is as follows. In order not to write repetitive code, the complete code will not be posted in the future, only the key code will be posted.
package main import "fmt" func main() { var name string = "Python Programming Time" (name) }
Judging from the rvalue (the value to the right of the equal sign, rvalue), it is obviously a string type (note here that double quotes are equivalent to single quotes in Python, but in Go, double quotes and single quotes are different. Here, double quotes are necessary to represent strings, while single quotes represent characters of type rune, which will be introduced separately in the future). Therefore, it can also be simplified to
var name = "Python programming"
If your rvalue has a decimal point, the compiler will declare your variable as float64 without specifying a type, but in many cases, we do not need such high precision (it takes up more memory space)
In this case, it is recommended to specify the type, and don't be lazy
var rate float32 0.89
The second type: multiple variables are declared together
Declaring multiple variables can be written as multiple lines as above, and can also be written as follows
var ( name string age int gender string )
The third type: declare and initialize a variable
Using := (Deduced declaration writing or short type declaration method: the compiler will automatically infer the corresponding type of the lvalue based on the rvalue type.), you can declare a variable and initialize it (explicitly).
name := "Python Programming" // Equivalent tovar name string = "Python Programming" // Equivalent tovar name = "Python Programming"
But this method has a limitation, it can only be used inside functions
Fourth: Declare and initialize multiple variables
name, age := "wangbm", 28
This method is also often used for variable exchange
var a int = 100 var b int = 200 b, a = a, b
The fifth type: new function declares a pointer variable
Here I will first talk about the related content of pointers.
Variables are divided into two types: ordinary variables and pointer variables
Ordinary variables store the data itself, while pointer variables store the address of the data.
The following code, age is a normal variable, the content stored is 28, and ptr is the memory address where the variable age value is stored: 0xc000010098
package main import "fmt" func main() { var age int = 28 var ptr = &age // & is followed by a variable name, indicating the memory address of the variable being retrieved ("age: ", age) ("ptr: ", ptr) }
Output
age: 28
ptr: 0xc000010098
The new function to be mentioned here is a traitor function in Go.
Using the expression new(Type) will create an anonymous variable of Type, initialize it to a zero value of Type, and then return the variable address, and the returned pointer type is *Type.
package main import "fmt" func main() { ptr := new(int) ("ptr address: ", ptr) ("ptr value: ", *ptr) // * A pointer variable is followed by, indicating that the value is retrieved from the memory address}
Output
ptr address: 0xc000010098
ptr value: 0
There is no difference between creating variables using new and creating variables using normal variable declaration statements. In addition to not declaring the name of a temporary variable, we can also use new(Type) in expressions. In other words, the new function is similar to a syntactic sugar, not a new basic concept.
The following two ways of writing can be said to be equivalent
// Use newfunc newInt() *int { return new(int) } // Use traditional methodsfunc newInt() *int { var dummy int return &dummy }
No matter which method above, variables/constants can only be declared once. If you declare them many times, an error will be reported when compiling.
But there are exceptions, which involves a special variable: anonymous variables, also known as placeholders, or blank identifiers, are represented by underscores.
Anonymous variables have three advantages:
- No memory allocation, no memory space occupied
- You don't need to be confused about naming useless variable names
- There will be no problems after multiple statements
Usually we use anonymous reception to receive values that must be received, but we will not use.
func GetData() (int, int) { return 100, 200 } func main(){ a, _ := GetData() _, b := GetData() (a, b) }
Replenish:This content mainly talks about 5 features of GO language fast:
- Value storage (effective value processing and value storage, less memory consumption);
- Function calls (using Inlining inline optimization technology)
- Forced garbage collection mechanism (escape analysis, if there is no reference, recycling);
- Go process
- Stack Management (If the old memory is too small, the new large stack will be reassigned and the old content will be copied into the new content)
Summarize
The above is the method of creating five variables in Go language introduced to you by the editor. I hope it will be helpful to you!