0
0
GoHow-ToBeginner · 3 min read

How to Pass Array to Function in Go: Syntax and Examples

In Go, you pass an array to a function by specifying the array type and size in the function parameter, like func f(arr [3]int). The function receives a copy of the array, so changes inside the function do not affect the original array unless you pass a pointer.
📐

Syntax

To pass an array to a function in Go, declare the function parameter with the array type and its fixed size. For example, func f(arr [3]int) means the function f accepts an array of exactly 3 integers.

The array is passed by value, so the function gets a copy. To modify the original array, pass a pointer like *[3]int.

go
package main

import "fmt"

func example(arr [3]int) {
    // arr is a copy of the original array
    arr[0] = 100
}

func main() {
    a := [3]int{1, 2, 3}
    example(a)
    fmt.Println(a) // prints [1 2 3], original array unchanged
}
💻

Example

This example shows passing an array to a function and printing its elements. It also shows that changes inside the function do not affect the original array.

go
package main

import "fmt"

func printArray(arr [3]int) {
    for i, v := range arr {
        fmt.Printf("Element %d: %d\n", i, v)
    }
    arr[0] = 100 // change inside function
}

func main() {
    myArray := [3]int{10, 20, 30}
    printArray(myArray)
    fmt.Println("After function call:", myArray)
}
Output
Element 0: 10 Element 1: 20 Element 2: 30 After function call: [10 20 30]
⚠️

Common Pitfalls

Passing by value: Arrays are copied when passed to functions, so changes inside the function do not affect the original array.

Array size mismatch: The function parameter array size must exactly match the argument array size, or the code will not compile.

To modify the original array: Pass a pointer to the array instead of the array itself.

go
package main

import "fmt"

// Wrong: size mismatch causes compile error
// func wrong(arr [2]int) {}

// Correct: matching size
func correct(arr [3]int) {
    arr[0] = 50
}

// To modify original array, use pointer
func modify(arr *[3]int) {
    arr[0] = 50
}

func main() {
    a := [3]int{1, 2, 3}
    correct(a)
    fmt.Println("After correct call:", a) // unchanged
    modify(&a)
    fmt.Println("After modify call:", a) // changed
}
Output
After correct call: [1 2 3] After modify call: [50 2 3]
📊

Quick Reference

ConceptSyntax ExampleNotes
Pass array by valuefunc f(arr [3]int)Function gets a copy; original unchanged
Pass pointer to arrayfunc f(arr *[3]int)Function can modify original array
Array size must matchfunc f(arr [3]int)Array size in parameter must equal argument size
Call with pointerf(&myArray)Pass address to modify original

Key Takeaways

Arrays are passed by value in Go, so functions get a copy, not the original.
Function parameter array size must exactly match the argument array size.
To modify the original array inside a function, pass a pointer to the array.
Passing slices is often easier for variable-length collections and modification.
Always use & to pass the address when passing a pointer to an array.