Arrays and Slices in Go
Arrays and slices are fundamental data structures in Go used for storing and manipulating ordered sequences of elements. While they share similarities, they have key differences in size and flexibility.
Arrays
Section titled “Arrays”Definition and Declaration
Section titled “Definition and Declaration”Arrays have a fixed size defined at declaration. They are declared using [size]type{values}:
numbers := [5]int{1, 2, 3, 4, 5}This creates an array numbers of size 5, initialized with values 1 to 5.
Accessing Elements
Section titled “Accessing Elements”Elements are accessed using their index within square brackets:
firstNumber := numbers[0] // firstNumber will be 1thirdNumber := numbers[2] // thirdNumber will be 3Index starts from 0, and accessing an out-of-bounds index leads to a panic.
Modifying Elements
Section titled “Modifying Elements”Elements can be modified using the same syntax:
numbers[2] = 10 // Change third element to 10Length and Capacity
Section titled “Length and Capacity”len(array)returns the number of elements in the array (always equal to the declared size).- Arrays don’t have a separate capacity concept.
Slices
Section titled “Slices”Definition and Creation
Section titled “Definition and Creation”Slices are dynamically-sized, flexible references to underlying arrays. They are created using:
[]type: Empty slice of a specific type.make([]type, length, capacity): Allocate a slice with specified length and capacity.- Slicing an existing array or slice:
slice := array[low:high].
// Empty sliceemptySlice := []string{}
// Slice with length and capacitynumbersSlice := make([]int, 3, 5) // Length 3, capacity 5
// Slicing an arraymyArray := [5]int{1, 2, 3, 4, 5}subSlice := myArray[1:4] // Subslice from index 1 to 4 (excluding 4)Accessing and Modifying Elements
Section titled “Accessing and Modifying Elements”Similar to arrays, elements are accessed and modified using their index:
firstElement := numbersSlice[0] // firstElement will be 0
subSlice[1] = 20 // Change second element of subSliceLength and Capacity
Section titled “Length and Capacity”len(slice)returns the number of elements in the slice.cap(slice)returns the capacity (maximum number of elements) of the underlying array.
Slicing a Slice
Section titled “Slicing a Slice”Slices can be sliced to create new sub-slices:
subSubSlice := subSlice[0:2] // Subslice of subSlice from index 0 to 2 (excluding 2)Appending Elements
Section titled “Appending Elements”The append function adds elements to the end of a slice and returns a new slice with the updated elements:
newSlice := append(numbersSlice, 10, 20) // Creates a new slice with numbersSlice + 10 and 20Built-in functions
Section titled “Built-in functions”append: Adds elements to the end of a slice.copy: Copies elements from one slice to another.cap: Returns the capacity of a slice.make: Creates a slice with specified length and capacity.
These are just some of the essential aspects of arrays and slices in Go. Remember to explore the documentation and experiment with code for a deeper understanding.
Multidimensional Arrays
Section titled “Multidimensional Arrays”Declaration and Access
Section titled “Declaration and Access”Multidimensional arrays are arrays of arrays. They are declared like:
grid := [3][4]int{ {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12},}Elements are accessed using multiple indices:
firstElement := grid[0][0] // firstElement will be 1thirdRow := grid[2] // thirdRow is a 1D array {9, 10, 11, 12}Slicing Multidimensional Arrays
Section titled “Slicing Multidimensional Arrays”Slices can be used with multidimensional arrays:
subGrid := grid[1:3] // Subgrid is a 2D slice containing second and third rowssubRow := grid[0][1:3] // Subrow is a slice of second and third elements in the first rowPointers and Slices
Section titled “Pointers and Slices”Passing Slices by Reference
Section titled “Passing Slices by Reference”Slices are passed by reference to functions, meaning changes within the function modify the original slice.
func modifySlice(s []int) { s[0] = 100}
numbers := []int{1, 2, 3}modifySlice(numbers)fmt.Println(numbers) // Output: [100 2 3]Modifying Slices within Functions
Section titled “Modifying Slices within Functions”- Use
appendto add elements to a slice within a function. - Be cautious when modifying the underlying array directly, as it can lead to unexpected behavior.
func appendToSlice(s []int, value int) { s = append(s, value)}
numbers := []int{1, 2, 3}appendToSlice(numbers, 4)fmt.Println(numbers) // Output: [1 2 3 4]Remember, these are just brief introductions to each subtopic. For deeper understanding, explore the Go documentation and experiment with code examples.