Go language is a safe and efficient language. It provides pointers as a data type, allowing programmers to more fully control the use of memory. Pointers are an important feature in the Go language, and mastering the use of pointers is a required course for every Go developer. This article will introduce how to use pointers in Go language.
Simply put, a pointer is a variable pointing to a memory address. For a variable, we can use the & operator to get its memory address, and the pointer variable is the variable that stores this memory address. Through pointers, we can directly access the value in the corresponding memory address and modify this value. This is the most basic functionality a pointer can provide.
In the Go language, declaring a pointer needs to be identified by using the * operator. For example:
var ptr *int // 声明一个指向int型变量的指针
A ptr variable is declared here, which is an integer pointer. That is to say, ptr stores the memory address of an integer variable.
The basic operations of pointers include obtaining addresses, converting types, obtaining values, etc.
Use the & operator to get the memory address of a variable, such as:
var a int = 10 var ptr *int // 声明一个指向int型变量的指针 ptr = &a // ptr指向a变量的内存地址
Here the address of the a variable is stored in the pointer variable ptr.
Pointer variables can be type converted, which is very useful when passing function parameters or any other operation that requires type conversion.
For example:
var a int = 20 var ip *int ip = &a fmt.Printf("变量a的地址为:%x ", &a) // 指针变量中存储的地址 fmt.Printf("ip 变量存储的地址:%x ", ip ) // 使用指针访问值 fmt.Printf("*ip 变量的值:%d ", *ip )
In the above code, it is shown again that the variable address is obtained, the pointer ip points to the address of a, and then the value of variable a is accessed through the *ip operator.
Through the * operator, we can get the value stored in the memory address pointed to by the pointer.
For example:
var a int = 20 // 声明一个int型变量a var ip *int // 声明一个int型指针变量ip ip = &a // 取a的地址,将地址存储到ip中 fmt.Printf("a 变量的地址是:%x ", &a ) // 输出a的地址 fmt.Printf("ip 变量存储的地址是:%x ", ip ) // 输出ip变量中存储的地址 fmt.Printf("*ip 变量的值是:%d ", *ip ) // 输出ip变量中存储的值
In actual applications, sometimes it is impossible to determine the initial value pointed by the pointer. In this case, the default null pointer should be used.
In the Go language, the default null pointer is represented by the nil keyword. The nil pointer is also called a null pointer. For example:
var ptr *int // 声明一个指向int型变量的指针,未赋初值 if ptr == nil { fmt.Printf("ptr是空指针 ") }
In the code, the ptr pointer variable is declared, but no value is assigned, and its value is nil. At this time, by comparing with nil, you can determine whether the ptr pointer variable is a null pointer.
Pointers are widely used in various scenarios in Go language. Below, several common application scenarios will be introduced.
When passing a pointer as a parameter in a function call, you can pass the address of the variable to the calling function, so that the value in the memory pointed to by the pointer can be modified in the function. , will directly affect variables outside the function.
For example:
func swap (x *int, y *int) { var temp int temp = *x // 保存x地址上的值 *x = *y // 将y赋值给x地址上的值 *y = temp // 将原来x的值赋值给y地址上的值 } func main() { // 声明两个变量 var a int = 100 var b int = 200 fmt.Printf("交换前,a 的值为:%d ", a ) fmt.Printf("交换前,b 的值为:%d ", b ) // 调用swap()函数交换值 swap(&a, &b) fmt.Printf("交换后,a 的值为:%d ", a ) fmt.Printf("交换后,b 的值为:%d ", b ) }
In the swap function, a pointer is used to receive the two parameters passed in, and the * operator is used to obtain the value in the memory pointed to by the pointer. After the exchange, the Assign value back. In the function call, pass in the addresses of &a and &b, so that modifications inside the function will directly affect the values of variables a and b in the main function.
Returning pointers in functions is also a very common application scenario. Just like returning an ordinary value, a function that returns a pointer can directly return the address of a pointer to somewhere in the program. For example:
func getPtr() *int { i := 1 return &i } func main() { p := getPtr() fmt.Println(*p) }
In the getPtr function, an i integer variable is declared, use the & operator to obtain the address of i, and return this address. Then in the program call, assign the return value to the p pointer variable, and then access the value of i through *p.
Different from ordinary variable arrays, pointer arrays store pointer variables. Through the index of the array, you can access the corresponding pointer variable, and then use the * operator to obtain the pointer The value at the memory address.
For example:
func main() { a := 1 b := 2 c := 3 ptrArr := [...]*int{&a, &b, &c} for _, ptr := range ptrArr { fmt.Println(*ptr) } }
In the code, three integer variables a, b, c are declared, and then the pointer array ptrArr is declared. Use... to represent the uncertain array length. In the corresponding location, the address of the corresponding variable is stored. In the program, the pointer array is looped and each element in the array is used as a pointer variable to obtain the value in the corresponding address.
This article mainly introduces the basic concepts and usage of pointers in Go language. Mastering the use of pointers can help developers better control memory usage and improve program performance and efficiency. In actual projects, pointers are used in a wide range of scenarios. Developers can use pointers in appropriate scenarios based on project needs. Although there are certain risks in using pointers, as long as you follow the principles of data security and standardized use, you can play the role of pointers and write code more efficiently.
The above is the detailed content of How are pointers used in Go language?. For more information, please follow other related articles on the PHP Chinese website!