Algorithm
In Go (also known as Golang), an array is a fixed-size sequence of elements of the same type. The size of an array is determined at the time of declaration and cannot be changed later. The general syntax for declaring an array in Go is as follows:
go
var arrayName [size]dataType
Here:
arrayName
is the name of the array.size
is the number of elements in the array.dataType
is the type of elements that the array can hold.
For example, you can declare an array of integers with a size of 5 as follows:
go
var numbers [5]int
This declares an array named numbers
that can hold 5 integers.
You can also initialize the values of an array at the time of declaration:
go
var numbers = [5]int{1, 2, 3, 4, 5}
In this example, the array is initialized with the values 1, 2, 3, 4, and 5.
Arrays in Go are zero-indexed, meaning the index of the first element is 0, the index of the second element is 1, and so on. You can access elements of an array using square brackets and the index:
go
fmt.Println(numbers[0]) // prints the first element (1)
fmt.Println(numbers[1]) // prints the second element (2)
It's important to note that arrays in Go have a fixed size, and their size is part of their type. This can make them less flexible compared to slices, another data structure in Go that allows for dynamic sizing.
Code Examples
#1 Example Program that demonstrates the use of arrays in Go:
Code -
Golang Programming
package main
import "fmt"
func main() {
// Declare and initialize an array
var numbers = [5]int{1, 2, 3, 4, 5}
// Access and print elements of the array
fmt.Println("Element at index 0:", numbers[0])
fmt.Println("Element at index 1:", numbers[1])
fmt.Println("Element at index 2:", numbers[2])
fmt.Println("Element at index 3:", numbers[3])
fmt.Println("Element at index 4:", numbers[4])
}
Copy The Code &
Try With Live Editor
#2 Accessing array elements in Golang
Code -
Golang Programming
// Program to access the array elements
package main
import "fmt"
func main() {
languages := [3]string{"Go", "Java", "C++"}
// access element at index 0
fmt.Println(languages[0]) // Go
// access element at index 2
fmt.Println(languages[2]) // C++
}
Copy The Code &
Try With Live Editor
#3 Initialize an Array in Golang
Code -
Golang Programming
package main
import "fmt"
func main() {
// declare an array
var arrayOfIntegers[3] int
// elements are assigned using index
arrayOfIntegers[0] = 5
arrayOfIntegers[1] = 10
arrayOfIntegers[2] = 15
fmt.Println(arrayOfIntegers)
}
Copy The Code &
Try With Live Editor
Output
#4 Initialize specific elements of an array
Code -
Golang Programming
package main
import "fmt"
func main() {
// initialize the elements of index 0 and 3 only
arrayOfIntegers := [5]int{0: 7, 3: 9}
fmt.Println(arrayOfIntegers)
}
Copy The Code &
Try With Live Editor
Output
#5 Changing the array element in Go
Code -
Golang Programming
// Program to change element by assigning the desired index with a new value
package main
import "fmt"
func main() {
weather := [3]string{"Rainy", "Sunny", "Cloudy"}
// change the element of index 2
weather[2] = "Stromy"
fmt.Println(weather)
}
Copy The Code &
Try With Live Editor
Output
#6 Find the length of an Array in Go Lang Programming
Code -
Golang Programming
package main
import "fmt"
func main() {
// create an array
var arrayOfIntegers = [...]int{1, 5, 8, 0, 3, 10}
// find the length of array using len()
length := len(arrayOfIntegers)
fmt.Println("The length of array is", length)
}
Copy The Code &
Try With Live Editor
Output
#7 Looping through an array in Go Lang Programming
Code -
Golang Programming
package main
import "fmt"
func main() {
age := [...]int{12, 4, 5}
// loop through the array
for i := 0; i < len(age); i++ {
fmt.Println(age[i])
}
}
Copy The Code &
Try With Live Editor
Output
4
5
#8 Multidimensional array in Golang Programing
Code -
Golang Programming
// Program to illustrate multidimensional array
package main
import "fmt"
func main() {
// create a 2 dimensional array
arrayInteger := [2][2]int{{1, 2}, {3, 4}}
// access the values of 2d array
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
fmt.Println(arrayInteger[i][j])
}
}
}
Copy The Code &
Try With Live Editor
Output
2
3
4
Demonstration
Go Lang Programming Array