golang pointer usage

WBOY
Release: 2023-05-19 09:02:36
Original
913 people have browsed it

Golang is an open source programming language with features such as efficiency, simplicity, and security. Therefore, it is widely used in web development, cloud computing, network programming, operating systems and other fields. In Golang, pointers are a very important concept. It is very important for developers to master the usage of Golang pointers.

1. Definition and declaration of pointers

In Golang, a pointer is a variable used to store the address of another variable. Because the pointer stores the address of the variable, not the value of the variable itself, the value of the pointer variable should be an address. A pointer also has its own type, and its type depends on the type of variable it points to. In Golang, the way to define and declare a pointer variable is as follows:

var ptr *int             // 定义一个指向整型的指针变量
var ptr1 *string         // 定义一个指向字符串的指针变量
Copy after login

The naming rules of pointer variables are the same as other variables, starting with a letter or underscore, and can contain letters, numbers, and underscores.

2. & and * Operators

In Golang, the & and operators are the core of pointer operations. The & operator is used to get the address of a variable, and the operator is used to get the value of the variable pointed to by the pointer variable. The following code shows how to use the & and * operators:

var num int = 10          // 定义一个整型变量
var ptr *int               // 定义一个指向整型的指针变量
ptr = &num               // 将num的地址赋值给ptr
fmt.Println("num的值:", num)            // 输出:10
fmt.Println("ptr的值:", ptr)            // 输出:0xc042036018
fmt.Println("ptr指向的值:", *ptr)      // 输出:10
Copy after login

First defines an integer variable num, and then defines a pointer variable ptr pointing to an integer. Use the & operator to get the address of num and assign it to the ptr variable. Finally, use the * operator to get the value of the variable pointed to by ptr.

3. nil pointer

In Golang, if a pointer variable is not initialized, its value will be set to nil, indicating that it points to an invalid address. The following is a simple example of using nil pointers:

var ptr *int          // 定义一个指向整型的指针变量
if ptr == nil {
   fmt.Println("ptr是一个空指针")
}
Copy after login

In this example code, we define a pointer variable ptr that points to an integer. Since no memory address is assigned to it, it is automatically set to nil. In the if statement, we check whether the pointer variable is initialized by judging whether ptr is empty.

4. Pointers as function parameters

In Golang, pointers can be used to pass function parameters. This way the values ​​of the original variables can be modified within the function instead of just copying them. The following is an example of using pointers as function parameters:

func main() {
   var num int = 10
   fmt.Println("调用函数前 num 的值:", num)    // 输出:10
   changeNum(&num)
   fmt.Println("调用函数后 num 的值:", num)    // 输出:20
}

func changeNum(p *int) {
   *p = 20
}
Copy after login

In this example, we define a function named changeNum, which has an integer pointer parameter p. In the main function, we define an integer variable num and pass its address to the changeNum function. In the changeNum function, we modify the value of the original num variable using the p pointer.

5. new function

In Golang, you can use the new function to allocate memory space for pointer variables on the heap. The new function returns a pointer to newly allocated memory. The following is an example of using the new function:

ptr := new(int)            // 为整型指针变量分配内存空间
*ptr = 10                // 给它指向的整数变量赋值
fmt.Println("ptr的值:", ptr)          // 输出:0xc042036030
fmt.Println("ptr指向的值:", *ptr)    // 输出:10
Copy after login

In this example, we use the new function to allocate memory space for an integer pointer variable ptr. We then operate on it, assign a value to the integer variable it points to, and print its value.

To sum up, pointers are a very important concept in Golang and are widely used in system programming and data structures. Mastering the usage of Golang pointers can help developers better understand the operating mechanism of Golang and write Golang programs more efficiently.

The above is the detailed content of golang pointer usage. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!