【英文】Go语言的指针

Preface

Notes on the study of pointers in the Go language

Declaration and Definition of Pointers

Declaring a Pointer Variable

  • Declare a pointer, which is initially a null pointer (nil), with a default value of nil, and a value of 0
  • Accessing a null pointer will result in an error
  • Values from 0 to 255 are system reserved and cannot be read or written to, accessing them will result in an error

data type: should correspond to the data type being stored

1
var p *data type

Defining a Pointer Variable

&variable: use the & operator to get the address of the variable and assign it to the pointer variable

1
var p *data type = &variable

Defining a Wild Pointer

  • The pointer variable points to an unknown memory address
  • Accessing a wild pointer will result in an error
1
var p *data type = 0xFFFFFFFF

Allocating Memory for a Pointer Variable

  • Allocate space in the heap area and assign the address value to the pointer
  • The size of the allocated memory is the size of the specified data type, with a default value of the default value of the data type
  • In Go language, you only need to allocate space, there is no need to free space
1
p = new(data type)

Pointer Operations

  • Pointer variables in Go language have the advantages of efficient pointer access without the risk of pointer offset and arithmetic, thus avoiding illegal modification of critical data

Taking the Address

  • Use &variable name to get the hexadecimal memory address value of the variable
1
2
var num int = 0
&num

Assigning Memory Address

  • Assign the memory address to be stored in the pointer
1
2
var p *int
p = &num

Dereferencing

  • Use *pointer variable to get or modify the actual data pointed to by the pointer

Getting the Value Pointed to by the Pointer

1
*p

Modifying the Value Pointed to by the Pointer

1
*p = 0

Address Passing of Function Parameters

  • Taking a function that swaps data as an example
1
2
3
func functionName(a *int, b *int) {
*a, *b = *b, *a
}

Array Pointers

Declaring an Array Pointer

1
var p *[array length]data type

Defining an Array Pointer

  • A pointer to an array
1
var p *[array length]data type = &array name

Accessing the Array Element via the Pointer

1
(*p)[array index]
  • Optimization of pointers by Go language
1
p[array index]

Slice Pointers

Declaring a Slice Pointer

1
var p *[]data type

Defining a Slice Pointer

1
var p *[]data type = &slice name

Accessing the Slice Element via the Pointer

1
(*p)[array index]

Getting the Length of the Slice via the Pointer

1
len(*p)

Iterating through the Slice via the Pointer

1
2
3
for k, v := range *p {
...
}

Outputting the Data Type

1
fmt.Printf("%T", p)

Outputting the Address

1
fmt.Printf("%p", p)

Struct Pointers

Declaring a Struct Pointer

1
var variable name *struct name

Defining a Struct Pointer

1
var variable name *struct name = &struct variable name

Accessing Struct Attributes via the Struct

1
(*p).attribute name = attribute value
1
p.attribute name = attribute value

Multilevel Pointers

Double Pointers

  • Pointers that store pointers
1
pp = &p

Done

References

Bilibili - Okaka Yeah Yeah
Bilibili - Python Luffy