Home > Article > Backend Development > The difference between variables and pointers in Go language: an analysis from a practical perspective
The difference between variables and pointers in Go language from a practical perspective
Introduction:
Variables and pointers are very important concepts in Go language. In practice Often used in development. This article will start from a practical perspective and use specific code examples to introduce the differences between variables and pointers, and explore their usage scenarios in the Go language.
1. Variables
In the Go language, variables are the basic unit for storing data in memory. Many beginners are already very familiar with the concept of variables, so I will briefly introduce the declaration and use of variables, and then focus on the variable delivery mechanism.
1.1 Declaration and use of variables
In the Go language, we can declare a variable through the var keyword, for example:
var num int // 声明了一个整型变量num num = 10 // 赋值 fmt.Println(num) // 输出10
In addition to using the var keyword, we can also use Use short variable declaration to declare a variable, for example:
num := 10 // 声明并初始化一个整型变量num fmt.Println(num) // 输出10
This method is more concise and the var keyword can be omitted.
1.2 Variable passing mechanism
In the Go language, there are two ways to pass variables: value passing and reference passing.
Value passing refers to copying the value of the actual parameter to the formal parameter during the function call. Modifications to the formal parameter in the function will not affect the value of the actual parameter. For example:
func change(num int) { num = 20 // 修改形式参数的值 } num := 10 // 声明并初始化一个整型变量num change(num) // 调用函数change fmt.Println(num) // 输出10
You can see that although the value of the formal parameter is modified in the change function, it does not affect the value of the actual parameter.
Passing by reference refers to passing the reference of the actual parameter to the formal parameter during the function call. Modification of the formal parameter in the function will affect the value of the actual parameter. In Go language, pointers are used to implement reference passing. We will discuss the use of pointers in detail in the next section.
2. Pointer
In Go language, a pointer is a variable that stores the memory address of another variable. Through pointers, we can access and modify the value of variables indirectly. Below we will discuss the use of pointers from two aspects: the declaration and use of pointers and the pointer passing mechanism.
2.1 Declaration and use of pointers
In Go language, we can use the * sign to declare a pointer variable, for example:
var ptr *int // 声明一个指向整型变量的指针ptr num := 10 // 声明并初始化一个整型变量num ptr = &num // 将num的地址赋值给ptr fmt.Println(*ptr) // 输出10,*ptr表示访问指针指向的值
Through *ptr we can access the value pointed to by the pointer , can also be modified.
2.2 Pointer passing mechanism
In the Go language, pointer passing can realize reference passing, so that the function's modification of the variable can affect the value outside the function. For example:
func change(ptr *int) { *ptr = 20 // 修改指针指向的值 } num := 10 // 声明并初始化一个整型变量num change(&num) // 调用函数change fmt.Println(num) // 输出20
You can see that through pointer passing, we modify the value pointed to by the pointer in the change function, thus affecting the value outside the function.
3. Usage scenarios of variables and pointers
In actual development, we need to choose to use variables or pointers according to different needs. A specific example is given below to illustrate this point.
Suppose we are developing a student management system, and each student contains two attributes: name and age. We define a structure to represent students:
type Student struct { Name string Age int }
In the function of adding students, we need to pass in a student object as a parameter. If value passing is used, a copy of the student object will be copied each time the add student function is called, which increases memory and performance overhead. Therefore, in this case, we can consider using pointers to pass student objects. The code is as follows:
func addStudent(student *Student) { // ... }
By using pointers, we can directly modify the value of the student object without additional copy operations.
Conclusion:
Through the above examples and discussions, we can draw the following conclusions:
Finally, it should be noted that when using pointers, you need to pay attention to whether the pointer variable is empty and whether the memory pointed to is released, to avoid errors.
The above is the detailed content of The difference between variables and pointers in Go language: an analysis from a practical perspective. For more information, please follow other related articles on the PHP Chinese website!