[Author: @mdimamhosen Date: 2025-04-19 Category: interview-qa/arrays Tags: [go, arrays, functions] ]
Arrays in Go
Declaring Arrays
You can declare an array in Go using the following syntax:
var arrayName [size]elementType
Example:
var numbers [5]int
Initializing Arrays
Arrays can be initialized at the time of declaration:
var numbers = [5]int{1, 2, 3, 4, 5}
Or you can use the shorthand notation:
numbers := [5]int{1, 2, 3, 4, 5}
Accessing Array Elements
Array elements are accessed using the index, which starts from 0:
fmt.Println(numbers[0]) // Output: 1
Iterating Over Arrays
You can iterate over arrays using a for
loop:
for i := 0; i < len(numbers); i++ {
fmt.Println(numbers[i])
}
Or using the range
keyword:
for index, value := range numbers {
fmt.Println(index, value)
}
Multidimensional Arrays
Go supports multidimensional arrays. A two-dimensional array is declared as follows:
var matrix [3][3]int
Example:
matrix := [3][3]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
Array of Arrays
You can also create an array of arrays:
var arrayOfArrays [2][3]int
Example:
arrayOfArrays := [2][3]int{
{1, 2, 3},
{4, 5, 6},
}
Passing Arrays to Functions
Arrays can be passed to functions by value, meaning the function receives a copy of the array:
func printArray(arr [5]int) {
for i := 0; i < len(arr); i++ {
fmt.Println(arr[i])
}
}
To modify the original array, you can pass a pointer to the array:
func modifyArray(arr *[5]int) {
arr[0] = 100
}
Frequently Asked Questions
Q1: How can I find the length of an array in Go?
You can use the built-in len()
function to find the length of an array.
Example:
package main
import "fmt"
func main() {
arr := [5]int{1, 2, 3, 4, 5}
fmt.Println("Length of the array:", len(arr)) // Output: 5
}
Q2: How do I copy an array in Go?
In Go, you can copy an array by simply assigning it to another array of the same type and size.
Example:
package main
import "fmt"
func main() {
original := [3]int{1, 2, 3}
copy := original
fmt.Println("Original:", original) // Output: [1 2 3]
fmt.Println("Copy:", copy) // Output: [1 2 3]
}
Q3: How can I pass an array to a function without copying it?
To avoid copying, you can pass a pointer to the array.
Example:
package main
import "fmt"
func modifyArray(arr *[3]int) {
arr[0] = 42
}
func main() {
arr := [3]int{1, 2, 3}
modifyArray(&arr)
fmt.Println("Modified array:", arr) // Output: [42 2 3]
}
Example code to test: main.go
package main
import "fmt"
func main() {
arr1 := [5]int{1, 2, 3, 4, 5}
arr2 := [5]int{1, 2, 3, 4, 5}
fmt.Println(arr1)
fmt.Println(arr2)
var arr3 [5]int
// fmt.Println(arr3) // this should print [0 0 0 0 0]
// println("Length:", len(arr3))
for i := 0; i < len(arr3); i++ {
fmt.Scan(&arr3[i])
}
fmt.Println(arr3)
strArr := [3]string{"one", "two", "three"}
for i := 0; i < len(strArr); i++ {
fmt.Print(strArr[i]+ "")
}
}