Home »
Golang
Go Language - Arrays
In Go, an array is a data structure that holds a fixed number of elements of the same type. It allows you to store multiple values in a single variable instead of declaring separate variables for each value.
Go Array Declaration
To declare an array in Go, specify the number of elements and their type as shown below:
var arrayName [size] elementType
For example, to declare an array of 8 integers named scores, use:
var scores [8] int
Here, scores is an array that can store up to 8 integer values.
Initializing an Array
You can initialize an array element by element or all at once:
var temperatures = [5]float64{32.5, 28.3, 30.0, 35.1, 29.8}
The number of values inside the curly braces {}
must not exceed the declared array size. If you omit the size, Go will automatically determine it:
var temperatures = []float64{32.5, 28.3, 30.0, 35.1, 29.8}
This also creates an array of size 5.
temperatures[2] = 31.0
This sets the 3rd element of the array to 31.0. Remember, the first index is 0.
Shorthand Array Declaration
Go provides a shorthand way to declare arrays without explicitly mentioning their type and size.
Syntax
arrayName := [arraySize]Type{val1, val2, val3, ...}
Example
Below is an example of shorthand array declaration and iterating over a string array in Go:
package main
import "fmt"
func main() {
// Declare an array using shorthand syntax
fruits := [3]string{"Mango", "Peach", "Grapes"}
fmt.Println("Fruit list:")
for i := 0; i < 3; i++ {
fmt.Println(fruits[i])
}
}
When executed, this program outputs:
Fruit list:
Mango
Peach
Grapes
Accessing Array Elements
To access an array element, use the array name followed by the index:
var speed float64 = velocities[4]
The above statement assigns the 5th element of the velocities array to the variable speed.
Example
Below is an example demonstrating declaration, assignment, and accessing array elements:
package main
import "fmt"
func main() {
var numbers [6]int // An array of 6 integers
// Assigning values
for i := 0; i < 6; i++ {
numbers[i] = i * 10
}
// Displaying values
for j := 0; j < 6; j++ {
fmt.Printf("Element[%d] = %d\n", j, numbers[j])
}
}
When executed, this program outputs:
Element[0] = 0
Element[1] = 10
Element[2] = 20
Element[3] = 30
Element[4] = 40
Element[5] = 50
Multi-Dimensional Arrays
Arrays with multiple dimensions allow storing data in tabular format.
Example
Below is an example of a two-dimensional array in Go:
package main
import "fmt"
func main() {
var matrix [2][3]int = [2][3]int{{1, 2, 3}, {4, 5, 6}}
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
fmt.Print(matrix[i][j], " ")
}
fmt.Println()
}
}
When executed, this program outputs:
1 2 3
4 5 6
Passing Arrays to Functions
Arrays can be passed to functions by value or reference.
Example
Below is an example of passing an array to a function in Go:
package main
import "fmt"
func printArray(arr [3]int) {
for _, v := range arr {
fmt.Println(v)
}
}
func main() {
numbers := [3]int{10, 20, 30}
printArray(numbers)
}
When executed, this program outputs:
10
20
30
Finding Array Length
You can use the len()
function to find the array length.
Example
Below is an example of finding the length of an array in Go using the len() function:
package main
import "fmt"
func main() {
numbers := [3]int{10, 20, 30}
fmt.Println(len(numbers))
}
When executed, this program outputs:
3
Advertisement
Advertisement