SoFunction
Updated on 2025-03-05

Detailed explanation of the use of pointers in Golang

1. Concept

Pointer: A variable pointing to a memory address, a pointer is used to store the memory address of the variable

Go language definition variables must declare data types, because data of different data types occupy different storage space, resulting in different memory address allocation sizes, and all pointers can only store memory addresses of variables of the same type.

Pointers are divided into two types: type pointers and slice pointers

Type pointers allow modification of data. You can directly pass data using pointers without copying data, but type pointers cannot be offset and operation.

A slice pointer is a slice type pointer that contains the original pointer, number of elements and capacity of the starting element.

  • Pointer: Also known as pointer variable, that is, the variable used to store memory addresses. Generally, the data format of the memory address is represented by 0xcXXXXXXX, such as 0xc0000180a8 or 0xc0000ac058, etc.
  • A pointer is a variable and has its own memory address. The memory address it stores is the memory address of another variable
  • Pointer type: It is the size of the memory address stored by the pointer. For example, pointer a is defined as int type, it can only store the memory address of integer variables. Therefore, when using pointers, you must declare the pointer type to ensure that the pointer can only store one data type.
  • Pointer assignment: Assign the memory address of a variable to a pointer, and use the address operator "&" in front of a variable to obtain the memory address of the variable.
  • Pointer value: Get the corresponding value from the pointer variable through the memory address of a variable. Just use the value operator "*" in front of the pointer variable.

2. Pointer definition and null pointer

Syntax format

var name *type

name represents the pointer variable name

type is the data type of pointer variables, such as data types built in Go language such as numbers, strings, and slices.

The null value of Go is represented by nil

Null pointer: The pointer is a null value

3. Pointer assignment and value

In Go programming, all variables are defined first and then used

// Define a variable of type int and assign a value of 200var name int = 200
// Define a pointer of type intvar ptr *int
// Assign the value of the pointer, assign the memory address of name to ptr, and use the address character "&"ptr = &name
// Pointer takes the value, use " * " before ptr to get the value of namename1 := *ptr
/*
     & Get out the memory address
     * Fetch the corresponding value according to the memory address
 */

4. Slice pointer

Slicing is a relatively special data structure that is easy to use and manage data collections

Slices are built around the concept of dynamic arrays and can be automatically grown and reduced as needed.

Slices can be understood as dynamic arrays, and the slice length is automatically adjusted according to the elements in the slice.

The slice pointer of Go language is represented by slices, and each element of the slice can only store memory addresses.

The syntax definition of slice pointer is as follows:

// Definition method onevar name []*type
// Definition method twoname := []*type{}
 

name represents the pointer variable name

type is the data type of pointer variables, such as numbers, strings, etc. The data type built in Go language

The slice pointer can store the memory addresses of multiple variables in the slice, which is convenient for managing multiple variables.

After the slice pointer is defined, if the initial value is not set, the default is empty. Since the slice is a dynamic array, its data length can be automatically adjusted. Go language will not allocate memory addresses, so the memory address of the slice pointer cannot be obtained through the address operator "&"

5. Pointer of pointer

Pointer: is a pointer variable pointing to another pointer variable, and another pointer variable pointing to a certain variable

Syntax format:

// Define the pointervar name **type
// Get the value of a variable from the pointer's pointerv := **name

name represents the pointer variable name, and is defined as a pointer with two value operators " * "

type is the data type of pointer variables, such as data types built in Go language such as numbers, strings, and slices.

v is to obtain the value of a variable from the pointer's pointer, and must be implemented using two value operators "*"

This is the end of this article about the detailed explanation of the use of pointers in Golang. For more related Golang pointers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!