Easy Tutorial
❮ Go Type Casting Go Constants ❯

Go Language Pointers

Pointers in Go are easy to learn, and using pointers in Go can simplify the execution of certain tasks.

Let's learn about Go pointers step by step.

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

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

The following example demonstrates the memory address of a variable:

Example

package main

import "fmt"

func main() {
   var a int = 10   

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

Executing the above code outputs:

Address of the variable: 20818a220

Now we have learned what a memory address is and how to access it. Next, we will introduce pointers in detail.


What is a Pointer?

A pointer variable points to a 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 * is used to specify that the variable is a pointer. Here are valid pointer declarations:

var ip *int        /* Pointer to an integer */
var fp *float32    /* Pointer to a float */

This example shows a pointer to an int and a float32.


How to Use Pointers

The process of using pointers:

To access the contents pointed to by a pointer, prefix the pointer type with *.

Example

package main

import "fmt"

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

   ip = &a  /* Store the address in the pointer variable */

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

   /* Address stored in pointer variable */
   fmt.Printf("Address stored in ip variable: %x\n", ip )

   /* Access the value using the pointer */
   fmt.Printf("Value of *ip variable: %d\n", *ip )
}

The output of the above example is:

Address of variable a: 20818a220
Address stored in ip variable: 20818a220
Value of *ip variable: 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.

nil conceptually is the same as null, None, nil, NULL in other languages, representing a zero or null value.

A pointer variable is commonly 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:

The value of ptr is : 0

Null pointer judgment:

if(ptr != nil)     /* ptr is not a null pointer */
if(ptr == nil)    /* ptr is a null pointer */

More on Go Pointers

Next, we will introduce more applications of pointers in Go:

Content Description
Go Pointer Array You can define an array of pointers to store addresses
Go Pointer to Pointer Go supports pointers to pointers
Go Passing Pointers to Functions Passing by reference or address allows changing values during function calls
❮ Go Type Casting Go Constants ❯