Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
Arrays are fundamental in most programming languages, and Go is no exception. Let's dive into arrays in Go.
In Go, an array is a fixed-size collection of elements of the same type. The size of the array is part of its type, which means once you've defined an array of a certain size, you can't change its size.
You can declare an array by specifying its size and element type:
var arr [5]int
This declares an array of 5 integers.
You can initialize an array at the time of declaration:
var arr [5]int = [5]int{1, 2, 3, 4, 5}
Or more simply:
arr := [5]int{1, 2, 3, 4, 5}
If you don't provide all the elements, the rest will have the zero value for the type:
arr := [5]int{1, 2, 3} // {1, 2, 3, 0, 0}
...
) for sizeYou can let Go determine the size of the array:
arr := [...]int{1, 2, 3, 4, 5} fmt.Println(len(arr)) // Outputs: 5
Array elements can be accessed using indices:
arr := [5]int{1, 2, 3, 4, 5} fmt.Println(arr[2]) // Outputs: 3 arr[2] = 99 fmt.Println(arr[2]) // Outputs: 99
You can use the for
loop to iterate over the array:
arr := [5]int{1, 2, 3, 4, 5} for i, v := range arr { fmt.Printf("Index: %d, Value: %d\n", i, v) }
Go supports multi-dimensional arrays:
var matrix [3][3]int // 3x3 matrix matrix[0] = [3]int{1, 2, 3} matrix[1] = [3]int{4, 5, 6} matrix[2] = [3]int{7, 8, 9}
Because of the fixed-size nature of arrays, they are less frequently used in Go compared to slices, which are more flexible and dynamic.
Arrays form the foundational building block upon which slices (a more flexible, dynamic, and commonly-used data structure) are built. While you might find yourself using slices more often due to their dynamic nature, it's essential to understand arrays as they underpin many aspects of how Go manages data in collections.
Golang Array Declaration and Initialization:
package main import "fmt" func main() { var numbers [5]int // Declaration with zero values names := [3]string{"Alice", "Bob", "Charlie"} // Declaration and initialization fmt.Println(numbers) // Output: [0 0 0 0 0] fmt.Println(names) // Output: [Alice Bob Charlie] }
How to Iterate Through Arrays in Golang:
for
loop to iterate through the elements of an array.package main import "fmt" func main() { numbers := [5]int{1, 2, 3, 4, 5} for i := 0; i < len(numbers); i++ { fmt.Println(numbers[i]) } }
Arrays vs Slices in Golang:
package main import "fmt" func main() { // Array arr := [3]int{1, 2, 3} // Slice slice := []int{1, 2, 3} fmt.Println(arr, slice) }
Multi-dimensional Arrays in Golang:
package main import "fmt" func main() { matrix := [2][3]int{ {1, 2, 3}, {4, 5, 6}, } fmt.Println(matrix) }
Dynamic Arrays in Golang:
package main import "fmt" func main() { // Using slices for dynamic behavior dynamicArray := []int{1, 2, 3} dynamicArray = append(dynamicArray, 4) fmt.Println(dynamicArray) // Output: [1 2 3 4] }
Array Length and Capacity in Golang:
package main import "fmt" func main() { numbers := [5]int{1, 2, 3, 4, 5} fmt.Println("Length:", len(numbers)) fmt.Println("Capacity:", cap(numbers)) }
Copying Arrays in Golang:
copy
function to copy elements from one array to another.package main import "fmt" func main() { source := [3]int{1, 2, 3} destination := [3]int{} copy(destination[:], source[:]) fmt.Println(destination) // Output: [1 2 3] }
Golang Array Indexing and Accessing Elements:
package main import "fmt" func main() { numbers := [5]int{10, 20, 30, 40, 50} fmt.Println(numbers[2]) // Output: 30 }
Sorting Arrays in Golang:
sort
package to sort arrays in Go.package main import ( "fmt" "sort" ) func main() { numbers := [5]int{5, 2, 8, 1, 6} sort.Ints(numbers[:]) fmt.Println(numbers) // Output: [1 2 5 6 8] }