Golang Tutorial

Fundamentals

Control Statements

Functions & Methods

Structure

Arrays & Slices

String

Pointers

Interfaces

Concurrency

Pointer to an Array as Function Argument in Golang

In Go, arrays are value types, which means when you pass an array to a function, it receives a copy of that array, not a reference to the original array. If you make changes to the array inside the function, it won't affect the original array. However, by using a pointer to an array, you can pass a reference to the original array, allowing you to modify it within the function.

Let's explore this with an example:

Passing an Array Without Pointers:

package main

import "fmt"

func modifyArray(arr [5]int) {
    for i := range arr {
        arr[i] = 0
    }
}

func main() {
    myArray := [5]int{1, 2, 3, 4, 5}
    modifyArray(myArray)
    fmt.Println(myArray) // [1 2 3 4 5]
}

In this example, after calling modifyArray, myArray remains unchanged because the function works on a copy.

Passing an Array Using Pointers:

package main

import "fmt"

func modifyArray(arr *[5]int) {
    for i := range arr {
        arr[i] = 0
    }
}

func main() {
    myArray := [5]int{1, 2, 3, 4, 5}
    modifyArray(&myArray)
    fmt.Println(myArray) // [0 0 0 0 0]
}

This time, after calling modifyArray, the contents of myArray are changed, because we passed a pointer to the original array.

A Few Things to Note:

  1. Syntax: When declaring a pointer to an array, you use the * prefix before the array type, like *[5]int.

  2. Dereferencing: In the function, you can access the array elements directly with the index, like arr[i], because Go automatically dereferences array pointers for you.

  3. Slices: In many cases, Go developers prefer using slices over arrays, because slices are more flexible and can be resized. When you pass a slice to a function, it inherently behaves like passing by reference due to the internal representation of slices (which includes a pointer to an array).

Conclusion:

Passing a pointer to an array as a function argument allows you to modify the original array within the function. This approach can be useful in specific scenarios where you need such behavior with arrays. However, in most situations in Go, you'll find that using slices offers a more flexible and idiomatic way to work with collections of data.

  1. Passing Array by Reference in Golang Function:

    • Arrays are passed by value in Golang, but you can simulate passing by reference using slices.
    • Example:
      package main
      
      import "fmt"
      
      func modifyArray(arr []int) {
          arr[0] = 100
      }
      
      func main() {
          myArray := []int{1, 2, 3}
          modifyArray(myArray)
          fmt.Println(myArray) // Output: [100 2 3]
      }
      
  2. Using Pointers to Arrays as Function Arguments in Golang:

    • Pass a pointer to the array to allow modifications within the function.
    • Example:
      package main
      
      import "fmt"
      
      func modifyArray(arrPtr *[3]int) {
          arrPtr[0] = 100
      }
      
      func main() {
          myArray := [3]int{1, 2, 3}
          modifyArray(&myArray)
          fmt.Println(myArray) // Output: [100 2 3]
      }
      
  3. Arrays and Pointers in Golang Function Parameters:

    • Arrays and pointers can be used interchangeably in function parameters for better flexibility.
    • Example:
      package main
      
      import "fmt"
      
      func modifyArray(arr interface{}) {
          switch arr := arr.(type) {
          case *[3]int:
              arr[0] = 100
          case []int:
              arr[0] = 200
          }
      }
      
      func main() {
          myArray := [3]int{1, 2, 3}
          mySlice := []int{4, 5, 6}
      
          modifyArray(&myArray)
          modifyArray(mySlice)
      
          fmt.Println(myArray) // Output: [100 2 3]
          fmt.Println(mySlice) // Output: [200 5 6]
      }
      
  4. Working with Pointer to Array Arguments in Golang:

    • Utilize pointers to arrays for more direct access to the array elements.
    • Example:
      package main
      
      import "fmt"
      
      func modifyArray(arrPtr *([3]int)) {
          arrPtr[0] = 100
      }
      
      func main() {
          myArray := [3]int{1, 2, 3}
          modifyArray(&myArray)
          fmt.Println(myArray) // Output: [100 2 3]
      }
      
  5. Passing Multidimensional Array Pointers to Functions in Golang:

    • Pass pointers to multidimensional arrays for efficient modification.
    • Example:
      package main
      
      import "fmt"
      
      func modifyArray(arrPtr *[2][3]int) {
          arrPtr[0][0] = 100
      }
      
      func main() {
          myArray := [2][3]int{{1, 2, 3}, {4, 5, 6}}
          modifyArray(&myArray)
          fmt.Println(myArray) // Output: [[100 2 3] [4 5 6]]
      }
      
  6. Benefits of Using Pointers to Arrays in Golang Functions:

    • Pointers to arrays provide a direct reference to the array, enabling efficient modification.
    • Example:
      package main
      
      import "fmt"
      
      func modifyArray(arrPtr *[3]int) {
          arrPtr[0] = 100
      }
      
      func main() {
          myArray := [3]int{1, 2, 3}
          modifyArray(&myArray)
          fmt.Println(myArray) // Output: [100 2 3]
      }
      
  7. Golang Pointer Arithmetic with Array Arguments:

    • Pointer arithmetic is not directly supported in Golang. However, array indexing serves a similar purpose.
    • Example:
      package main
      
      import "fmt"
      
      func modifyArray(arrPtr *[3]int) {
          arrPtr[1] = 200
          // Pointer arithmetic not directly supported
      }
      
      func main() {
          myArray := [3]int{1, 2, 3}
          modifyArray(&myArray)
          fmt.Println(myArray) // Output: [1 200 3]
      }
      
  8. Handling Array Sizes Dynamically with Array Pointers in Golang:

    • Golang arrays have a fixed size, but slices can be used for dynamic sizes.
    • Example:
      package main
      
      import "fmt"
      
      func modifySlice(arr []int) {
          arr[0] = 100
      }
      
      func main() {
          mySlice := []int{1, 2, 3}
          modifySlice(mySlice)
          fmt.Println(mySlice) // Output: [100 2 3]
      }
      
  9. Passing Arrays vs Pointers to Arrays in Golang Functions:

    • Choose between passing the array directly or a pointer based on your use case and requirements.
    • Example:
      package main
      
      import "fmt"
      
      func modifyArray(arr [3]int) {
          arr[0] = 100
      }
      
      func modifyArrayPointer(arrPtr *[3]int) {
          arrPtr[1] = 200
      }
      
      func main() {
          myArray := [3]int{1, 2, 3}
          modifyArray(myArray)
          fmt.Println(myArray) // Output: [1 2 3]
      
          modifyArrayPointer(&myArray)
          fmt.Println(myArray) // Output: [1 200 3]
      }