Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
In Go (Golang), arrays are value types, which means when they are passed to a function, a copy of the original array is passed, not a reference to it. Because of this, any modifications to the array within the function do not reflect outside of it.
However, you can overcome this behavior by using slices or pointers if you need to modify the original array from within a function.
Here's a tutorial on how to pass an array to a function in Go:
package main import "fmt" // Define a function that accepts an array of 5 integers func modifyArray(arr [5]int) { for i := range arr { arr[i] = i * i // Modify the array } fmt.Println("Inside modifyArray: ", arr) } func main() { arr := [5]int{1, 2, 3, 4, 5} modifyArray(arr) fmt.Println("Inside main: ", arr) // The original array remains unchanged }
package main import "fmt" // Define a function that accepts a pointer to an array of 5 integers func modifyArray(arr *[5]int) { for i := range arr { arr[i] = i * i // Modify the array } fmt.Println("Inside modifyArray: ", *arr) } func main() { arr := [5]int{1, 2, 3, 4, 5} modifyArray(&arr) // Pass the address of the array fmt.Println("Inside main: ", arr) // The original array gets modified }
In most practical scenarios in Go, slices are preferred over arrays because they are more flexible and can be resized. When you pass a slice to a function, you are essentially passing a reference, so any modifications inside the function reflect on the original slice.
package main import "fmt" // Define a function that accepts a slice func modifySlice(s []int) { for i := range s { s[i] = i * i // Modify the slice } fmt.Println("Inside modifySlice: ", s) } func main() { slice := []int{1, 2, 3, 4, 5} modifySlice(slice) fmt.Println("Inside main: ", slice) // The original slice gets modified }
Arrays in Go are value types, so by default, they are passed to functions by value (i.e., a copy is passed).
You can modify the original array inside a function by passing a pointer to it.
Slices, which are reference types, can be used if you want modifications within a function to reflect outside. In most situations, slices are more idiomatic and flexible than arrays in Go.
Golang pass array to function example:
Description: Passing an array to a function in Golang.
Code:
package main import "fmt" func printArray(arr [3]int) { for _, value := range arr { fmt.Println(value) } } func main() { array := [3]int{1, 2, 3} printArray(array) }
Passing arrays as parameters in Golang functions:
Description: Passing arrays as parameters to Golang functions.
Code:
package main import "fmt" func modifyArray(arr [3]int) { for i := range arr { arr[i] *= 2 } } func main() { array := [3]int{1, 2, 3} modifyArray(array) fmt.Println("Modified Array:", array) }
How to pass a slice to a function in Golang:
Description: Passing a slice to a function in Golang.
Code:
package main import "fmt" func modifySlice(slice []int) { for i := range slice { slice[i] *= 2 } } func main() { slice := []int{1, 2, 3} modifySlice(slice) fmt.Println("Modified Slice:", slice) }
Working with arrays as function arguments in Golang:
Description: Demonstrating how to work with arrays as function arguments in Golang.
Code:
package main import "fmt" func processArray(arr [5]int) int { sum := 0 for _, value := range arr { sum += value } return sum } func main() { array := [5]int{1, 2, 3, 4, 5} result := processArray(array) fmt.Println("Sum of Array:", result) }
Golang array passing by reference or value:
Description: Understanding whether Golang arrays are passed by reference or value.
Code:
package main import "fmt" func modifyArray(arr [3]int) { arr[0] = 100 } func main() { array := [3]int{1, 2, 3} modifyArray(array) fmt.Println("Original Array:", array) }
Passing fixed-size arrays vs slices to functions in Golang:
Description: Highlighting the difference between passing fixed-size arrays and slices to functions in Golang.
Code:
package main import "fmt" func modifyArray(arr [3]int) { arr[0] = 100 } func modifySlice(slice []int) { slice[0] = 100 } func main() { array := [3]int{1, 2, 3} modifyArray(array) fmt.Println("Modified Array:", array) slice := []int{1, 2, 3} modifySlice(slice) fmt.Println("Modified Slice:", slice) }
Passing multidimensional arrays to functions in Golang:
Description: Passing multidimensional arrays to functions in Golang.
Code:
package main import "fmt" func processMatrix(matrix [2][3]int) { for _, row := range matrix { for _, value := range row { fmt.Print(value, " ") } fmt.Println() } } func main() { matrix := [2][3]int{ {1, 2, 3}, {4, 5, 6}, } processMatrix(matrix) }
Using pointers to pass arrays to functions in Golang:
Description: Utilizing pointers to pass arrays to functions in Golang.
Code:
package main import "fmt" func modifyArray(arr *[3]int) { for i := range arr { arr[i] *= 2 } } func main() { array := [3]int{1, 2, 3} modifyArray(&array) fmt.Println("Modified Array:", array) }