SoFunction
Updated on 2025-03-05

Golang pointer operation and commonly used pointer functions

Pointer operation

In Go, pointers are a very important type that can be used to pass the address of a variable rather than the variable itself.

Define pointer

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

var ptr *int

Get variable address

You can use the & operator to get the address of a variable, for example:

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

In this example, the ptr variable contains the address of variable a.

Dereference pointer

Use the * operator to dereference the pointer and access the variables pointed to by the pointer. For example, the following is an example of using pointers to access variables:

var a int = 10
var ptr *int = &a
("The value of a is:", a)
("The value of *ptr is:", *ptr)

In this example, *ptr will access the variable pointed to by ptr, that is, the value of a.

Pointer as function parameter

Pointers can be used as parameters in a function, so that the value of the variable passed when calling the function can be modified. For example:

func swap(x *int, y *int) {
    var temp int
    temp = *x    /* Save the value of x address */
    *x = *y      /* Assign y to x */
    *y = temp    /* Assign temp to y */
}
func main() {
    /* Define local variables */
    var a int = 100
    var b int= 200
    ("Value of a before swap: %d\n", a )
    ("Value of b before swap: %d\n", b )
    /* Call the swap() function
      * &a points to the address of the variable a
      * &b points to the address of the b variable
      */
    swap(&a, &b)
    ("The value of a after swap: %d\n", a )
    ("Value of b after swap: %d\n", b )
}

In this example, the swap() function uses a pointer as an argument, so that the values ​​of variables a and b passed in the main() function can be modified.

Null value of pointer

In Go, pointers can be empty, 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 does not point to any valid memory address. Then, we use the & operator to get the address of the variable x and assign it to the pointer p. Finally, we use the * operator to dereference p to get the value stored in the address it points to and store it in the variable y.

Please note that in Go language, pointers cannot perform arithmetic operations, nor can they be added or subtracted directly from integers. In addition, before accessing the value pointed to by the pointer, you must make sure that the pointer is not nil, otherwise it will cause a runtime error.

Commonly used pointer functions

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

new function

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

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

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

make function

The make function is used to create an object of type slicing, mapping, or channel and return a reference to that object. Here is an example:

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

Here, an integer slice of length 5 is created using the make function and assigned to the variable s. Then, through the subscript operation, the elements in the slice are assigned and output operations.

append function

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

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

Here we first create an empty integer slice and add three elements 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. It receives two parameters, the first parameter is the slice to be copied to, and the second parameter is the slice to be copied. Here is an example:

func main() {
    s1 := []int{1, 2, 3}
    s2 := []int{4, 5, 6}
    copy(s1, s2)
    (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 commonly used pointer functions above, there are some other commonly used pointer operations, such as address character &, pointer dereference character *, pointer operator + and -, etc. When using pointers, 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.

This is the article about Golang pointer operation and commonly used pointer functions. For more related Golang pointer and pointer function content, please search for my previous article or continue browsing the related articles below. I hope you will support me in the future!