Go Arrays
Go Arrays
In Go, an array is a fixed-size collection of elements of the same data type. It provides a way to store multiple values under a single variable name. Arrays in Go have a predefined length, meaning their size cannot be changed after declaration.
Declaring an Array in Go
To define an array in Go, you specify its length and the type of elements it will store.
Syntax:
var arrayName [size]dataType
Alternatively, you can use shorthand notation:
arrayName := [size]dataType{values}
Examples of Go Arrays
1. Basic Array Declaration and Initialization
package main import "fmt" func main() { var num[5]int // Declaring an integer array of size 5 num[0] = 60 // Assigning values num[1] = 70 num[2] = 80 num[3] = 90 num[4] = 100 fmt.Println("Elements in the array:", num) // Output: [60 70 80 90 100] }
2. Declaring and Initializing an Array in One Line
package main import "fmt" func main() { fruits := [3]string{"Apple", "Banana", "Cherry"} // Array with predefined values fmt.Println("Fruits:", fruits) // Output: [Apple Banana Cherry] }
3. Using [...] to Let the Compiler Determine Array Size
package main import "fmt" func main() { primes := [...]int{2, 3, 5, 7, 11} // Compiler automatically sets the size fmt.Println("List of prime numbers:", primes) // Output of this code is : [2 3 5 7 11] }
4. Iterating Over an Array
An array can be iterated using either a for loop or a range loop.
Using a for loop:
package main import "fmt" func main() { numbers := [4]int{10, 20, 30, 40} for i := 0; i < len(numbers); i++ { fmt.Println("Element at index", i, ":", numbers[i]) } }
Using a range loop:
package main import "fmt" func main() { cities := [3]string{"New York", "Paris", "Tokyo"} for index, value := range cities { fmt.Println("Index:", index, "Value:", value) } }
5. Modifying Array Elements
package main import "fmt" func main() { colors := [3]string{"Red", "Green", "Blue"} colors[1] = "Yellow" // Modifying element at index 1 fmt.Println("Updated Colors:", colors) // Output: [Red Yellow Blue] }
6. Multi-Dimensional Arrays
Go also supports multi-dimensional arrays.
package main import "fmt" func main() { matrix := [2][2]int{ {1, 2}, {3, 4} } for i := 0; i < 2; i++ { for j := 0; j < 2; j++ { fmt.Print(matrix[i][j], " ") } fmt.Println() } }
Output:
1 2 3 4
7. Copying Arrays
Since arrays are value types, assigning one array to another copies its contents.
package main import "fmt" func main() { i := [3]int{15, 20, 30} j := i // Copying array i to j j[0] = 100 fmt.Println("Original:", i) // Output: [15 20 30] fmt.Println("Copy:", j) // Output: [100 20 30] }
8. Comparing Arrays
Go allows comparing arrays of the same size and type using the == operator.
package main import "fmt" func main() { arr1 := [3]int{1, 2, 3} arr2 := [3]int{1, 2, 3} arr3 := [3]int{4, 5, 6} fmt.Println(arr1 == arr2) // Output: true fmt.Println(arr1 == arr3) // Output: false }
9. Array Length Using len() Function
package main import "fmt" func main() { numbers := [5]int{10, 20, 30, 40, 50} fmt.Println("Array Length:", len(numbers)) // Output: 5 }
Key Points About Go Arrays
- Fixed Size: Once an array is declared with a specific length, it cannot be resized.
- Zero-Value Initialization: If no values are provided, arrays are initialized with default values (e.g., 0 for integers, "" for strings).
- Value Type Behavior: Assigning an array to another variable creates a copy, not a reference.
- Use range for Iteration: The range keyword helps iterate over arrays easily.
When to Use Arrays in Go?
- Use when a collection of elements with a fixed size is required.
- When working with small datasets that won’t change in size.
- For performance-critical applications, where fixed memory allocation is beneficial.
However, in most cases, slices are preferred over arrays due to their flexibility. Slices provide a more dynamic alternative to arrays.
PreviousNext