Menu
Coddy logo textTech

Declaring Pointer Variables

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

In Go, you can declare pointers in two main ways: using var with a pointer type or using the address operator with :=.

Declare a pointer using var with a pointer type:

func main() {
    var x int = 42
    var ptr *int    // Declare a pointer to an integer
    ptr = &x        // Assign the address of x to ptr
    
    fmt.Println("Value of x:", x)
    fmt.Println("Value of ptr:", ptr)
    fmt.Println("Value at ptr:", *ptr)
}

The output shows the pointer stores the address of x:

Value of x: 42
Value of ptr: 0xc000018030
Value at ptr: 42

You can also declare and initialize a pointer in one line using :=:

y := 100
ptr := &y    // Declare and initialize pointer in one step
challenge icon

Challenge

Beginner

In this challenge, you'll practice declaring and using pointers in Go. A pointer is already set up for you, and your task is to create a new pointer that points to the same value.

Complete the TODO in the code to declare a new pointer variable called 'secondPointer' that points to the same memory address as the existing pointer.

Cheat sheet

In Go, you can declare pointers using var with a pointer type or using the address operator with :=.

Declare a pointer using var:

var x int = 42
var ptr *int    // Declare a pointer to an integer
ptr = &x        // Assign the address of x to ptr

Declare and initialize a pointer in one line using :=:

y := 100
ptr := &y    // Declare and initialize pointer in one step

Access pointer values:

  • ptr - the memory address
  • *ptr - the value at that address

Try it yourself

package main

import "fmt"

func main() {
	// Here's a variable and a pointer to it
	originalValue := 42
	pointerToValue := &originalValue
	
	// TODO: Declare a new pointer variable called 'secondPointer' 
	// that points to the same memory address as 'pointerToValue'
	
	
	// Don't modify the code below
	fmt.Printf("Original value: %v\n", originalValue)
	fmt.Printf("Value through first pointer: %v\n", *pointerToValue)
	fmt.Printf("Value through second pointer: %v\n", *secondPointer)
	
	// Let's change the original value and see all pointers reflect the change
	originalValue = 100
	fmt.Printf("\nAfter changing original value to 100:\n")
	fmt.Printf("Original value: %v\n", originalValue)
	fmt.Printf("Value through first pointer: %v\n", *pointerToValue)
	fmt.Printf("Value through second pointer: %v\n", *secondPointer)
}
quiz iconTest yourself

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

All lessons in Fundamentals