Menu
Coddy logo textTech

Dereferencing Pointers

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

Dereferencing pointers (*) lets you access the value stored at a memory address. This is called "dereferencing" the pointer.

Create a variable and a pointer to it:

func main() {
    number := 42
    pointer := &number
    
    // Dereference the pointer to get the value
    value := *pointer
    
    fmt.Println("Pointer:", pointer)
    fmt.Println("Dereferenced value:", value)
}

The output shows both the pointer and the value it points to:

Pointer: 0xc000018030
Dereferenced value: 42

The asterisk (*) before a pointer variable accesses the actual value at that memory address.

challenge icon

Challenge

Beginner

In this challenge, you'll practice dereferencing pointers in Go. We have a variable message with a string value, and a pointer messagePtr that points to it.

Your task is to dereference the pointer to access and print the value it points to.

Cheat sheet

Use the asterisk (*) operator to dereference a pointer and access the value at the memory address:

number := 42
pointer := &number

// Dereference the pointer to get the value
value := *pointer

fmt.Println("Pointer:", pointer)           // 0xc000018030
fmt.Println("Dereferenced value:", value)  // 42

The * operator before a pointer variable accesses the actual value stored at that memory address.

Try it yourself

package main

import "fmt"

func main() {
	// Here's our string variable
	message := "Hello, pointers!"
	
	// This is a pointer to our message variable
	messagePtr := &message
	
	// TODO: Dereference the messagePtr to get the value it points to
	// and store it in the variable 'value'
	value := ""
	
	// Print the results
	fmt.Println("The pointer points to the value:", value)
}
quiz iconTest yourself

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

All lessons in Fundamentals