Go Language Pointers

Pointers in Go are easy to learn, and using pointers in Go can make some tasks simpler.

Next, let's learn Go pointers step by step.

We all know that a variable is a convenient placeholder used to reference a computer memory address.

The address-of operator in Go is &, and when placed before a variable, it returns the memory address of that variable.

The following example demonstrates the address of variables in memory:

Example

package main

import "fmt"

func main() {
   var a int = 10  

   fmt.Printf("Address of variable: %x\n\n", &a  )
}

The output of executing the above code is:

变量的地址: 20818a220

Now we have understood what memory addresses are and how to access them. Next, we will introduce pointers in detail.


What is a pointer

A pointer variable points to the memory address of a value.

Similar to variables and constants, you need to declare a pointer before using it. The pointer declaration format is as follows:

var var_name *var-type

var-type is the pointer type, var_name is the pointer variable name, and the * sign is used to specify that the variable is a pointer. The following are valid pointer declarations:

var ip *int        /* 指向整型*/
var fp *float32    /* 指向浮点型 */

In this example, this is a pointer to int and float32.


How to Use Pointers

Pointer Usage Process:

  • Define a pointer variable.
  • Assign a value to the pointer variable.
  • Access the value at the address pointed to by the pointer variable.

Add the * sign (prefix) before the pointer type to get the content pointed to by the pointer.

Example

package main

import "fmt"

func main() {
   var a int= 20   /* Declare an actual variable */
   var ip *int        /* Declare a pointer variable */

   ip = &a  /* The address stored in the pointer variable */

   fmt.Printf("Address of variable a is: %x\n\n", &a  )

   /* The address stored in the pointer variable */
   fmt.Printf("Pointer address stored in variable ip: %x\n\n", ip )

   /* Use pointer to access value */
   fmt.Printf("Value of *ip variable: %d\n\n", *ip )
}

The output of the above example is:

a 变量的地址是: 20818a220
ip 变量储存的指针地址: 20818a220
*ip 变量的值: 20

Go Null Pointer

When a pointer is defined but not assigned to any variable, its value is nil.

A nil pointer is also called a null pointer.

Conceptually, nil is the same as null, None, nil, and NULL in other languages, all referring to zero values or empty values.

A pointer variable is often abbreviated as ptr.

See the following example:

Example

package main

import "fmt"

func main() {
   var  ptr *int

   fmt.Printf(The value of ptr is : %x\n", ptr  )
}

The output of the above example is:

ptr 的值为 : 0

Null Pointer Check:

if(ptr != nil)     /* ptr 不是空指针 */
if(ptr == nil)    /* ptr 是空指针 */

More on Go pointers

Next, we will introduce more pointer applications in Go:

content Description
Go array of pointers You can define an array of pointers to store addresses.
Go pointer to pointer Go supports pointers to pointers.
Go passing pointer parameters to functions By passing parameters by reference or address, you can change their values during function calls.
other extensions