Menu
Coddy logo textTech

The Address-Of Operator

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

The address-of operator (&) gets the memory address of a variable. It's how we create pointers in Go.

Create a variable and get its memory address:

func main() {
    number := 42
    addressOfNumber := &number
    
    fmt.Println("Value:", number)
    fmt.Println("Address:", addressOfNumber)
}

The output shows the value and its memory address:

Value: 42
Address: 0xc000018030

The address (like 0xc000018030) is a hexadecimal number representing the memory location where the value is stored. This address will be different each time you run the program.

challenge icon

Challenge

Beginner
In this challenge, you'll practice using the address-of operator (&) in Go. The address-of operator returns the memory address of a variable, which is where the variable's value is stored in the computer's memory.

Your task is to use the address-of operator to get the memory address of the provided variable and store it in the pointer variable that's already declared.

Cheat sheet

The address-of operator (&) gets the memory address of a variable and is used to create pointers in Go.

number := 42
addressOfNumber := &number

fmt.Println("Value:", number)
fmt.Println("Address:", addressOfNumber)

The address is displayed as a hexadecimal number (like 0xc000018030) representing the memory location where the value is stored. This address will be different each time you run the program.

Try it yourself

package main

import "fmt"

func main() {
	// This is our sample variable
	name := "Gopher"
	
	// This declares a pointer variable to store the address of a string
	var namePointer *string
	
	// TODO: Use the address-of operator (&) to get the memory address of 'name'
	// and store it in the 'namePointer' variable
	
	// Print the value that the pointer points to
	fmt.Printf("The value at that memory address is: %v\n", *namePointer)
}
quiz iconTest yourself

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

All lessons in Fundamentals