Published
-
Go Battle concepts
Content
Arrays and Slices
Arrays | Slices |
---|---|
Fixed at compile-time | Dynamic, can grow and shrink |
Includes size in type | Does not include size in type |
Rarely used in Go | Commonly used in Go |
Can waste memory if unused size | More efficient for variable data |
Arrays - [...]int{} or [n]int{} | []int{} |
Go Array example | Go slice example |
// Array declaration
var numbers [5]int // Fixed size of 5 integers
grades := [3]int{98, 93, 87} // Initialize with values
matrix := [2][3]int{{1, 2, 3}, {4, 5, 6}} // Multi-dimensional array
// Slice declaration
var scores []int // Zero-value slice (nil)
names := []string{"Pranshu", "Hritik", "Amit"} // Create slice with values
numbers := make([]int, 5) // Create slice with length 5
numbers := make([]int, 5, 10) // Create slice with length 5, capacity 10
Slice declaration
1. From an Arrays
var arr = [5]int{1, 2, 3, 4, 5}
var slice = arr[1:4] // Create a slice of arr from index 1 to 3.
fmt.Println(slice) // Output: [2 3 4]
2. From make
slice := make([]int, 3, 5) // Length 3, capacity 5.
fmt.Println(slice) // Output: [0 0 0]
3. using slice literals
slice := []int{10, 20, 30}
fmt.Println(slice) // Output: [10 20 30]
operations on Slices
- appending to a slice
slice := []int{1, 2, 3}
slice = append(slice, 4, 5)
fmt.Println(slice) // Output: [1 2 3 4 5]
- Copying Slices
source := []int{1, 2, 3}
dest := make([]int, len(source))
copy(dest, source)
fmt.Println(dest) // Output: [1 2 3]
more detail on arrays and slice
1. Arrays ([...]int{} or [n]int{})
have a fixed size.
- You cannot add or remove elements after declaration.
- You can only modify existing elements.
2. Slices ([]int{})
are dynamic.
- You can append or modify elements.
- Slices are more commonly used in Go for their flexibility.
Length vs Capacity
Length(len) | Slice(cap) |
---|---|
Number of elements in the slice | Maximum number of elements the slice can grow to without reallocating memory |
function - len() | function - cap() |
// Create a slice with length 3 and capacity 5
numbers := make([]int, 3, 5)
fmt.Printf("Length: %d, Capacity: %d\n", len(numbers), cap(numbers))
// Output: Length: 3, Capacity: 5
// The slice has 3 zero-value elements
fmt.Println(numbers)
// Output: [0 0 0]
// Append will add elements without reallocation until capacity is reached
numbers = append(numbers, 4)
fmt.Printf("After append - Length: %d, Capacity: %d\n", len(numbers), cap(numbers))
// Output: Length: 4, Capacity: 5
// When we exceed capacity, Go will allocate a new array with larger capacity
numbers = append(numbers, 5, 6)
fmt.Printf("After exceeding capacity - Length: %d, Capacity: %d\n", len(numbers), cap(numbers))
// Output: Length: 6, Capacity: 10 (Go typically doubles the capacity)
Behavior When Appending
- When appending in slice and when exceed capacity, Go automatically allocate a new array with larger capacity.
- The new capacity is typically doubled, but this depends on the implementation.