Operation of Golang pointers and commonly used pointer functions

Table of contents

pointer operation

define pointer

get variable address

dereference pointer

pointers as function arguments

pointer to null

Commonly used pointer functions

new function

make function

append function

copy function


pointer operation

In the Go language, a pointer is a very important type that can be used to pass the address of a variable instead of the variable itself.

define pointer

In the Go language, *operators are used to define pointers. For example, the following is the syntax for defining an integer pointer:

var ptr *int

get variable address

The address of a variable can be obtained using &the operator, for example:

var a int = 10
var ptr *int = &a

In this example, ptrthe variable contains athe address of the variable.

dereference pointer

Use *the operator to dereference a pointer, accessing the variable pointed to by the pointer. For example, the following is an example of accessing a variable using a pointer:

var a int = 10
var ptr *int = &a

fmt.Println("a 的值为:", a)
fmt.Println("*ptr 的值为:", *ptr)

In this example, the variable pointed to, which is the value of , *ptrwill be accessed .ptra

pointers as function arguments

You can use pointers as parameters in functions, so that you can modify the value of the variable passed when calling the function. For example:

func swap(x *int, y *int) {
    var temp int
    temp = *x    /* 保存 x 地址的值 */
    *x = *y      /* 将 y 赋值给 x */
    *y = temp    /* 将 temp 赋值给 y */
}

func main() {
    /* 定义局部变量 */
    var a int = 100
    var b int= 200

    fmt.Printf("交换前 a 的值 : %d\n", a )
    fmt.Printf("交换前 b 的值 : %d\n", b )

    /* 调用 swap() 函数
     * &a 指向 a 变量的地址
     * &b 指向 b 变量的地址
     */
    swap(&a, &b)

    fmt.Printf("交换后 a 的值 : %d\n", a )
    fmt.Printf("交换后 b 的值 : %d\n", b )
}

In this example, swap()the function takes a pointer as a parameter, so that the value of the variable and main()passed in the function can be modified .ab

pointer to null

In Go language, pointers can be null, for example:

var ptr *int = nil

In this example, we create a pointer p to a variable of type int. We initialize p to nil, which means it doesn't point to any valid memory address. We then use the & operator to take the address of the variable x and assign it to the pointer p. Finally, we dereference p using the * operator to get the value stored at the address it points to and store it in the variable y.

Note that in the Go language, pointers cannot perform arithmetic operations, nor can they be directly added to or subtracted from integers. Additionally, you must ensure that the pointer is not nil before accessing the value pointed to by the pointer, otherwise a runtime error will result.

Commonly used pointer functions

In the Go language, pointers are a very important concept that can be used to manipulate memory in programs and improve program efficiency. In addition to basic pointer arithmetic, there are some commonly used pointer functions that can be used to manipulate pointers.

new function

The new function is used to create a pointer to a type and return the address of the pointer. Here is an example:

func main() {
    var p *int
    p = new(int)
    *p = 10
    fmt.Println(*p)
}

Here, a pointer of type int is created using the new function and assigned to the variable p. Then assign and output the variable through the pointer p.

make function

The make function is used to create an object of type slice, map or channel and returns a reference to the object. Here is an example:

func main() {
    var s []int
    s = make([]int, 5)
    s[0] = 1
    fmt.Println(s)
}

Here use the make function to create an integer slice with a length of 5 and assign it to the variable s. Then use the subscript operation to assign and output the elements in the slice.

append function

The append function is used to add elements to the slice. It can receive one or more arguments, each of which is an element to add to the slice. Here is an example:

func main() {
    var s []int
    s = append(s, 1)
    s = append(s, 2, 3, 4)
    fmt.Println(s)
}

Here an empty integer slice is first created and three elements are added using the append function. Finally, the slice after adding elements is output.

copy function

The copy function is used to copy the contents of one slice into another slice. It takes two parameters, the first parameter is the slice to copy to, and the second parameter is the slice to copy to. Here is an example:

func main() {
    s1 := []int{1, 2, 3}
    s2 := []int{4, 5, 6}
    copy(s1, s2)
    fmt.Println(s1)
}

Here two integer slices s1 and s2 are created, and the contents of s2 are copied into s1. Finally, the copied s1 is output.

In addition to the above commonly used pointer functions, there are other commonly used pointer operations, such as: take address symbol &, pointer dereference symbol *, pointer operator + and -, etc. When using a pointer, you need to pay attention to whether the pointer is empty and whether the pointer points to a valid memory address. At the same time, you also need to pay attention to the life cycle of the pointer to avoid problems such as wild pointers.

Guess you like

Origin blog.csdn.net/weixin_71646897/article/details/130750155