Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
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:
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.
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.
Syntax: When declaring a pointer to an array, you use the *
prefix before the array type, like *[5]int
.
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.
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).
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.
Passing Array by Reference in Golang Function:
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] }
Using Pointers to Arrays as Function Arguments in Golang:
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] }
Arrays and Pointers in Golang Function Parameters:
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] }
Working with Pointer to Array Arguments in Golang:
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] }
Passing Multidimensional Array Pointers to Functions in Golang:
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]] }
Benefits of Using Pointers to Arrays in Golang Functions:
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] }
Golang Pointer Arithmetic with Array Arguments:
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] }
Handling Array Sizes Dynamically with Array Pointers in Golang:
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] }
Passing Arrays vs Pointers to Arrays in Golang Functions:
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] }