Menu
Coddy logo textTech

What is a Pointer?

Part of the Fundamentals section of Coddy's GO journey — lesson 60 of 109.

A pointer is a variable that stores the memory address of another variable. Pointers help us access or modify variables indirectly.

Declaring a pointer variable: Use the * symbol before the type to declare a variable that can hold a pointer:

var pointerToInt *int       // Declares a pointer variable that can point to an int
var pointerToString *string // Declares a pointer variable that can point to a string

Creating a pointer to an existing variable: Use the & (address-of) operator to get the memory address of a variable:

x := 10
pointerToX := &x  // pointerToX now holds the memory address of x
fmt.Println(pointerToX)

Accessing the value a pointer points to: Use the * (dereference) operator:

fmt.Println(*pointerToX)  // Prints the value stored at the address

The output will be:

0xc000018030  // Memory address (your address will differ)
10           // The value at that address

Cheat sheet

A pointer is a variable that stores the memory address of another variable.

Use the & (address-of) operator to get a pointer to a variable:

x := 10
pointerToX := &x  // pointerToX holds the memory address of x

Use the * (dereference) operator to access the value a pointer points to:

fmt.Println(*pointerToX)  // Prints the value stored at the address

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals