How to Pass Arrays to Functions in Go

1. Introduction

Passing an array to a function in Go copies the array and passes that copy to the function. This means changes made to the array inside the function do not affect the original array. However, if an array pointer is passed, the function can modify the original array. This post will demonstrate how to pass arrays to functions in Go.

2. Program Steps

1. Define an array.

2. Pass the array to a function by value and attempt to modify it.

3. Pass the array to another function by reference and modify it.

4. Use slices to demonstrate an efficient way to pass arrays to functions without copying them.

3. Code Program

package main

import "fmt"

// modifyArrayByValue attempts to modify the array but only changes the local copy
func modifyArrayByValue(arr [3]int) {
	arr[0] = 90
	fmt.Println("Inside modifyArrayByValue, arr is:", arr)

// modifyArrayByReference modifies the array by dereferencing the pointer
func modifyArrayByReference(arr *[3]int) {
	(*arr)[0] = 90
	fmt.Println("Inside modifyArrayByReference, arr is:", *arr)

// useSlice accepts a slice argument, providing an efficient way to pass an array
func useSlice(slice []int) {
	slice[0] = 90
	fmt.Println("Inside useSlice, slice is:", slice)

func main() {
	// Step 1: Define an array
	originalArray := [3]int{10, 20, 30}

	// Step 2: Pass the array to the function by value
	fmt.Println("After modifyArrayByValue, originalArray is:", originalArray)

	// Step 3: Pass the array to the function by reference
	fmt.Println("After modifyArrayByReference, originalArray is:", originalArray)

	// Step 4: Use a slice to pass the array efficiently
	slice := originalArray[:]
	fmt.Println("After useSlice, originalArray is:", originalArray)


Inside modifyArrayByValue, arr is: [90 20 30]
After modifyArrayByValue, originalArray is: [10 20 30]
Inside modifyArrayByReference, arr is: [90 20 30]
After modifyArrayByReference, originalArray is: [90 20 30]
Inside useSlice, slice is: [90 20 30]
After useSlice, originalArray is: [90 20 30]


1. package main - The package declaration.

2. import "fmt" - Importing the format package for I/O operations.

3. modifyArrayByValue function takes an array as a parameter, modifies it, and prints it. The modification does not affect the original array because the array is passed by value.

4. modifyArrayByReference function takes a pointer to an array, modifies the array it points to, and prints it. The modification reflects in the original array because the array is passed by reference.

5. useSlice function takes a slice, which is a reference to an underlying array, modifies it, and prints it. The modification affects the original array because slices are reference types.

6. originalArray is declared and initialized in the main function.

7. modifyArrayByValue is called with originalArray, but it does not change the original array.

8. modifyArrayByReference is called with a pointer to originalArray, and it changes the original array.

9. useSlice is called with a slice referencing originalArray, and it also changes the original array.

10. The fmt.Println statements are used to print the state of the array after each function call.

11. The output demonstrates the difference between passing an array by value and by reference and shows how slices provide a more efficient way to work with arrays in functions.