Module: Pointers

Pointer Basics

Go Programming: Pointers - Basics

Pointers are a fundamental concept in Go (and many other programming languages) that allow you to work directly with memory addresses. Understanding pointers is crucial for writing efficient and powerful Go code.

What is a Pointer?

A pointer is a variable that stores the memory address of another variable. Instead of holding a value directly, it holds the location in memory where that value is stored.

Think of it like this:

  • Variable: A house. It contains something (the value).
  • Pointer: An address to the house. It tells you where the house is.

Declaring Pointers

To declare a pointer, you use the * (asterisk) symbol before the type of the variable the pointer will point to.

package main

import "fmt"

func main() {
	var x int = 10
	var p *int // p is a pointer to an integer

	fmt.Println(x) // Output: 10
	fmt.Println(p) // Output: <nil> (because it hasn't been assigned an address yet)
}
  • var p *int: This declares a variable p that is a pointer to an integer. The *int signifies this.
  • <nil>: When a pointer is declared but not assigned an address, its value is nil. nil means the pointer doesn't point to anything.

The Address-of Operator (&)

To get the memory address of a variable, you use the & (ampersand) operator.

package main

import "fmt"

func main() {
	x := 10
	p := &x // p now holds the memory address of x

	fmt.Println(x) // Output: 10
	fmt.Println(p) // Output: 0xc0000140a8 (the actual address will vary)
}
  • p := &x: This assigns the memory address of x to the pointer p. Now p "points to" x.

Dereferencing Pointers (*)

To access the value stored at the memory address a pointer holds, you dereference the pointer using the * operator.

package main

import "fmt"

func main() {
	x := 10
	p := &x

	fmt.Println(*p) // Output: 10 (dereferencing p to get the value of x)

	*p = 20 // Change the value at the address pointed to by p (which is x)
	fmt.Println(x) // Output: 20 (x has been modified)
}
  • *p: This dereferences the pointer p. It retrieves the value stored at the memory address that p holds.
  • *p = 20: This assigns the value 20 to the memory location pointed to by p. Since p points to x, this effectively changes the value of x.

Key Concepts & Summary

  • Pointer Type: A pointer's type is determined by the type of the variable it points to. *int is a pointer to an integer, *string is a pointer to a string, etc.
  • & (Address-of Operator): Returns the memory address of a variable.
  • * (Dereference Operator): Accesses the value stored at the memory address held by a pointer.
  • nil: A special value indicating that a pointer doesn't point to any valid memory location.
  • Modifying Values: Changing the value through a pointer directly modifies the original variable.

Why Use Pointers?

  • Efficiency: Passing pointers to functions can be more efficient than passing large data structures by value, as it avoids copying the entire structure.
  • Modifying Original Values: Pointers allow functions to modify the original variables passed to them.
  • Dynamic Memory Allocation: Pointers are essential for working with dynamically allocated memory (e.g., using new).
  • Data Structures: Pointers are fundamental to building complex data structures like linked lists, trees, and graphs.

This covers the basic concepts of pointers in Go. Further exploration will involve topics like pointer arithmetic (which is limited in Go for safety reasons), and their use in more advanced scenarios. Remember to practice using pointers to solidify your understanding.