DEV Community

Cover image for Pointers in Go Explained
Fred
Fred

Posted on

Pointers in Go Explained

If you’re new to Go or programming in general, you may have come across pointers and felt a bit intimidated. I’ve been there! When I first encountered pointers, I saw the ampersand (&) and asterisk (*) symbols and instantly thought, What is happening here?

In this post, we’re going to break down pointers with a simple analogy and see how they work in Go. By the end, you’ll understand what pointers do and why they’re powerful tools in programming.

What are Pointers?

Imagine you’re looking at a map that shows the addresses of houses. Each house has an address, but that address doesn’t tell you anything about what’s inside the house—only where it’s located. In programming, variables are like those houses: each one has a memory address, where its data is stored.

though bubble with word pointer inside
A pointer holds the address of a variable, not the actual data inside the variable. It’s like writing down the address of a house so you can find it later.

So, What is a Variable?

A variable is the named spot in memory where data is stored. You can think of it as a specific house with a unique address on our map.
We use variables names because they are easy for us to remember, while memory addresses are often n hexadecimal form like 0x564B4.

package main

import "fmt"

func main(){
    var balance int = 35
    fmt.Println("Age:",age)  //Output the value of balance
    fmt.Println("Memory Address:", &age)//Output the address of balance
}
Enter fullscreen mode Exit fullscreen mode
  • balance is a variable, that holds the value 35
  • &balance is the memory address of balance. which Go represents as a hexadecimal numbers like 0x50C

Working with Pointers in Go

In Go, you use the ampersand (&) to get the address of a variable, and the asterisk (*) to access the value at that address.

1.Declaring a Pointer

You can create a pointer that points to an address of a variable. Here's how:

package main

import "fmt"

func main() {
    balance := 35             // Our variable
    balancePtr := &balance        // Pointer to the memory address of balance

    fmt.Println("Balance:", balance)             // 35
    fmt.Println("Pointer:", balancePtr)      // Memory address of balance (e.g., 0x50C)
    fmt.Println("Value at Pointer:", *balancePtr) // 35, value at that address
}
Enter fullscreen mode Exit fullscreen mode

Breakdown:

  • balance := 35 creates an integer variable with a value of 35.
  • balancePtr := &balance creates a pointer balancePtr that holds the address of balance.
  • *balancePtr lets us access the value stored at balancePtr's address, which is 35.

Why Use Pointers?

Pointers allow you to work with data directly in memory, without making copies. This is useful for larger data structures or when modifying data across functions.

Example of a function that modifies a variable's value:

package main

import "fmt"

func updateBalance(balancePtr *int) {
    *balancePtr = 40  // Change the value at the address
}

func main() {
    balance := 35
    fmt.Println("Before:", balance) // 35

    updateBalance(&balance)  // Pass the address of balance to the function
    fmt.Println("After:", balance)  // 40, since updateBalance modified the value directly
}
Enter fullscreen mode Exit fullscreen mode

The breakdown:

  • updateBalance takes a pointer balancePtr as a parameter.
  • *balancePtr = 40 changes the value at memory held by balancePtr.
  • In main, when we pass &balance to updateBalance, updateBalance directly changes balance's value without returning anything.

Key Points

  • Pointer store memory addresses, not values.
  • & gives you the address of a variable.
  • * allows you to access the value at an address.
  • Pass by reference: By passing pointers to functions(as parameters), you can modify variables directly without returning them.

The End

Pointers may feel strange at first, but they become second nature with practice. Using pointers efficiently can improve memory usage and program performance.Understanding pointers will make you a stronger Go developer and give you more control over how data flows in your programs.

2 Gopher mascot with words bye bye

Top comments (2)

Collapse
 
programmerraja profile image
Boopathi

This is a great explanation of pointers in Go! The analogy with houses and addresses makes it super easy to grasp the concept. I especially appreciate the clear examples and breakdown of how to use pointers in practice.

Collapse
 
githaiga22 profile image
Allan Githaiga

well i confess pointers has been quite hectic for me back then as a newbie but i would recommend this blog for beginners.Great job Fred for making it simpler 👏