Arrays are fundamental data structures used to store a collection of elements of the same type. In Go, an array is a fixed-size sequence of elements of a specific type. This means that once an array is declared with a certain size, it cannot be resized. Understanding arrays is crucial as they form the basis for more complex data structures like slices.
In Go, arrays are defined by specifying the element type and the number of elements it can hold. The syntax to declare an array is straightforward:
var arr [size]type
Here, size represents the number of elements in the array, and type specifies the data type of each element.
Arrays can be initialized at the time of declaration. There are several ways to do this:
{}.new function: This allocates memory for an array and returns a pointer to it.Elements in an array can be accessed using their index, which starts at 0. The syntax is:
arr[index]
Arrays are zero-indexed, meaning the first element is at index 0, the second element is at index 1, and so on.
Let's explore some practical examples to understand how arrays work in Go.
package main
import "fmt"
func main() {
// Declare an array of 5 integers
var numbers [5]int
// Initialize the array with values
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
fmt.Println(numbers) // Output: [10 20 30 40 50]
}
In this example, we declare an array of 5 integers and initialize it with some values. The `fmt.Println` function is used to print the entire array.
### Example 2: Literal Initialization
```go
package main
import "fmt"
func main() {
// Declare and initialize an array using literal syntax
colors := [3]string{"red", "green", "blue"}
fmt.Println(colors) // Output: [red green blue]
}
Here, we use the literal syntax to declare and initialize an array of strings. This is a concise way to specify initial values for the array elements.
### Example 3: Using `new` Function
```go
package main
import "fmt"
func main() {
// Allocate memory for an array of 4 integers using new
arr := new([4]int)
// Access and modify elements using the pointer
(*arr)[0] = 100
(*arr)[1] = 200
(*arr)[2] = 300
(*arr)[3] = 400
fmt.Println(*arr) // Output: [100 200 300 400]
}
In this example, we use the new function to allocate memory for an array of 4 integers. The new function returns a pointer to the array, so we need to dereference it using the * operator to access and modify its elements.
package main
import "fmt"
func main() {
// Declare and initialize an array of integers
numbers := [5]int{1, 2, 3, 4, 5}
// Iterate over the array using a for loop
for i := 0; i < len(numbers); i++ {
fmt.Println("Element at index", i, "is", numbers[i])
}
}
Here, we iterate over an array of integers using a for loop. The len function is used to get the length of the array, which helps us determine the loop's range.
After mastering arrays, you should move on to learn about slices in Go. Slices are more flexible than arrays and provide dynamic resizing capabilities, making them a preferred choice for most scenarios involving collections of elements.