Go Language Chapter 5 (Pointers)

Go Language Chapter 5 (Pointers)

First of all, if you don't have a compiler, you can type code through this URL: Lightly

Introduction

In the Go language, a pointer is a special data type that is used to store the memory address of a variable. By manipulating pointers, the values ​​of variables can be directly accessed and modified, enabling more efficient and flexible programming.

Declare and use pointers

In Go language, you can use *the symbol to declare a pointer variable, and use &the symbol to get the address of a variable. For example:

var ptr *int
var num int = 10
ptr = &num

The above code defines an integer pointer variable ptr, and initializes it to the address of the variable num. Note that a pointer must be initialized before it can be used.

Pointer variables can also be declared and initialized using the short variable declaration syntax:

num := 10
ptr := &num

This code example has the same effect as the code example above.

If you want to access the value of the variable pointed to by the pointer, you need to use *the operator . For example:

var ptr *int
var num int = 10
ptr = &num
fmt.Printf("num 变量的值为 %d\n", *ptr)

The above code will output "The value of the num variable is 10".

pointers as function arguments

In the Go language, functions can accept pointers as parameters, and modify the variable pointed to by the pointer inside the function. For example:

func swap(a, b *int) {
    
    
    tmp := *a
    *a = *b
    *b = tmp
}

func main() {
    
    
    var a, b int = 1, 2
    fmt.Printf("交换前:a=%d, b=%d\n", a, b)
    swap(&a, &b)
    fmt.Printf("交换后:a=%d, b=%d\n", a, b)
}

The above code defines a function called swap, which accepts two integer pointers as parameters, and uses the * operator to modify the variable pointed to by the pointer. In the main function, we define two integer variables a and b, and initialize them to 1 and 2 respectively. Then, we call the swap function, passing it the addresses of a and b as parameters. Finally, we output the values ​​of the swapped a and b variables.

It should be noted that when using a pointer, it must be ensured that the pointer is not nil, otherwise a runtime error may occur. Also, if the variable pointed to by the pointer is deleted or moved, the pointer becomes invalid.

example

1. Write a function to add 10 to a given integer variable, and implement it using pointers.

package main

import "fmt"

func addTen(num *int) {
    
    
    *num += 10
}

func main() {
    
    
    var num int = 10
    fmt.Printf("加 10 前,num 的值为 %d\n", num)
    addTen(&num)
    fmt.Printf("加 10 后,num 的值为 %d\n", num)
}

operation result:
insert image description here

2. Exchange the values ​​of the given two integer variables, using pointers.

package main

import "fmt"

func swap(a, b *int) {
    
    
    tmp := *a
    *a = *b
    *b = tmp
}

func main() {
    
    
    var a, b int = 1, 2
    fmt.Printf("交换前:a=%d, b=%d\n", a, b)
    swap(&a, &b)
    fmt.Printf("交换后:a=%d, b=%d\n", a, b)
}

operation result:
insert image description here

3. Modify the values ​​of all elements in the given slice, and output the modified result, using pointers.

package main

import "fmt"

func modify(arrPtr *[]int) {
    
    
    for i := 0; i < len(*arrPtr); i++ {
    
    
        (*arrPtr)[i] *= 2
    }
}

func main() {
    
    
    arr := []int{
    
    1, 2, 3, 4, 5}
    fmt.Println("修改前:", arr)
    modify(&arr)
    fmt.Println("修改后:", arr)
}

operation result:
insert image description here

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/130109109