Passing Pointers to Functions in Go

Pointers are variables that store memory addresses. They point to the location of a value rather than holding the value itself. In Go, pointers are powerful tools for managing memory efficiently and for passing references to data structures rather than making copies.

Introduction

  • Pointers in Go: Briefly explain that pointers are variables that store memory addresses of other variables. They allow indirect access and modification of data.
  • Passing by Value: Go functions, by default, pass arguments by value. This means a copy of the original data is passed, and changes made within the function do not affect the original variable.
  • Passing Pointers: To modify the original data from within a function, you need to pass pointers. A pointer contains the memory address of the variable, allowing the function to directly access and modify the original data.

Syntax and Examples

  • Declaring Pointers: Use the * operator before the data type to declare a pointer variable. For example, var intPtr *int.
  • Obtaining Address: Use the & (address-of) operator to get the memory address of a variable. For example, intPtr = &myInt.
  • Passing Pointers as Function Arguments: Declare the function parameter as a pointer type. For example, func modifyValue(ptr *int) { ... }.
  • Dereferencing Pointers: Use the * operator before a pointer to access the value it points to. For example, *intPtr = 10.
				
					package main

func main() {
    myInt := 5
    modifyValue(&myInt) // Pass address of myInt
    fmt.Println(myInt)   // Output: 10 (modified inside the function)
}

func modifyValue(ptr *int) {
    *ptr = *ptr * 2  // Dereference ptr and modify the value at that address
}

				
			
  • myInt is declared as an integer with value 5.
  • modifyValue takes a pointer to an integer (*int) as its argument.
  • In main, the address of myInt is obtained using & and passed to modifyValue.
  • Inside modifyValue, the pointer is dereferenced using *ptr to access the actual value at the memory address.
  • The value is doubled and assigned back to the memory location using *ptr.
  • Back in mainfmt.Println prints the modified value of myInt (10).

Advanced Concepts

Pointers to Pointers: You can create pointers to pointers, allowing for indirect access to data through multiple layers of pointers.

				
					package main

func main() {
    myInt := 5
    doubleValue(&myInt)
    fmt.Println(myInt)   // Output: 20 (modified through doubleValue)
}

func doubleValue(ptr **int) {
    *ptr = *ptr * 2  // Dereference twice to access and modify the value
}

				
			
  • myInt is declared as an integer with value 5.
  • doubleValue takes a pointer to a pointer to an integer (**int) as its argument.
  • In main, the address of myInt is obtained twice using & and passed to doubleValue.
  • Inside doubleValue, the pointer is dereferenced twice:
    • *ptr accesses the first pointer, which points to the memory address of myInt.
    • *(*ptr) further dereferences to access the actual value at that address.
  • The value is doubled and assigned back to the memory location using *(*ptr).
  • Back in mainfmt.Println prints the modified value of myInt (20).

Function Pointers: Function pointers store the memory address of a function, enabling you to pass functions as arguments to other functions and create function-like behavior dynamically.

				
					package main

func main() {
    var myFunc func(int) int

    if true {
        myFunc = square  // Assign square function to myFunc
    } else {
        myFunc = cube     // Assign cube function to myFunc
    }

    result := myFunc(5)
    fmt.Println(result) // Output: 25 (square of 5)
}

func square(x int) int {

				
			

The provided Go code defines a `main` function where:
– A function variable named `myFunc` is declared to hold references to functions that take an integer and return an integer.
– Within an if-else block, `myFunc` is conditionally assigned to reference either the `square` or `cube` function. However, since the condition is always true, `myFunc` is assigned to reference the `square` function.
– The `myFunc` variable is then invoked with an argument of `5`, resulting in the execution of the `square` function and storing the result (25) in the variable `result`.
– Finally, the result is printed to the standard output.

Understanding passing pointers in Go is essential for effectively modifying data within functions and working with complex data structures. By mastering these concepts, you can write more efficient, flexible, and expressive Go code. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India