index

Understanding Memory Pointers in Go with Uruguay

Uruguay is a country that - contraty to what most people believe - it does not have a name. Uruguay’s name in spanish is “República Oriental del Uruguay” which means, The Republic at the Eastern side of the Uruguay River - in programming, this is a pointer to a location rather than an actual name.

Because of this, Uruguay example is a perfect analogy for understanding pointers in Go. Pointers in Go are references to memory locations rather than the values themselves.

Here’s a concise guide:

package main

import "fmt"

func main() {
	// Uruguay is a country whose name is a reference to its location
	// Similarly, a pointer is a variable that references a memory location

	// Direct value (like saying "Uruguay" informally)
	countryName := "República Oriental del Uruguay"

	// Creating a pointer (a reference to where the actual name is stored)
	countryNamePtr := &countryName

	fmt.Println("The actual name:", countryName)
	fmt.Println("Memory address where name is stored:", countryNamePtr)
	fmt.Println("Accessing name through pointer:", *countryNamePtr)

	// Modifying value through pointer
	*countryNamePtr = "Eastern Republic of Uruguay"
	fmt.Println("Modified name:", countryName)

	// Function demonstrating pointers
	displayAndChange(&countryName)
	fmt.Println("Name after function call:", countryName)

	// Real-world example with a struct
	country := Country{
		OfficialName: "República Oriental del Uruguay",
		CommonName:   "Uruguay",
		Location:     "East of Uruguay River",
	}

	updateLocation(&country)
	fmt.Println("Updated country info:", country)
}

func displayAndChange(namePtr *string) {
	fmt.Println("In function - received pointer to:", *namePtr)
	*namePtr = "Uruguay (modified by function)"
}

type Country struct {
	OfficialName string
	CommonName   string
	Location     string
}

func updateLocation(c *Country) {
	c.Location = "Eastern Bank of Uruguay River"
}

Key concepts:

  1. Creating pointers:

    • Use & to get a memory address (pointer) to a variable
    • Like how “Uruguay” is a reference to the actual country
  2. Dereferencing pointers:

    • Use * to access the value at a memory address
    • Similar to knowing that when someone says “Uruguay,” they’re referring to “República Oriental del Uruguay”
  3. Passing by reference:

    • Enables functions to modify original values, not copies
    • Functions get a “reference” to change the real thing
  4. Pointer receivers in methods:

    • Allow methods to modify their receiver
    • Like having authority to rename Uruguay officially
  5. Nil pointers:

    • Represent a pointer that doesn’t point to anything
    • Like referring to a country that doesn’t exist

This guide demonstrates how pointers provide references to memory locations, just as Uruguay’s name provides a reference to its geographic location.