Modify Slice in Go #
Slices in Go are dynamic and flexible. You can access elements, modify them, append new items, and even copy slices to improve memory efficiency.
1. Access Elements of a Slice #
You can access a specific slice element by referring to its index. Go indexes start at 0.
Example:
package main
import ("fmt")
func main() {
prices := []int{10, 20, 30}
fmt.Println(prices[0]) // First element
fmt.Println(prices[2]) // Third element
}
Result:
10
30
2. Change Elements of a Slice #
You can change a slice element by referring to its index.
Example:
package main
import ("fmt")
func main() {
prices := []int{10, 20, 30}
prices[2] = 50 // Change third element
fmt.Println(prices[0])
fmt.Println(prices[2])
}
Result:
10
50
3. Append Elements to a Slice #
Use the append()
function to add elements at the end of a slice.
Syntax:
slice_name = append(slice_name, element1, element2, ...)
Example:
package main
import ("fmt")
func main() {
myslice1 := []int{1, 2, 3, 4, 5, 6}
fmt.Printf("myslice1 = %v\n", myslice1)
myslice1 = append(myslice1, 20, 21)
fmt.Printf("myslice1 = %v\n", myslice1)
}
Result:
myslice1 = [1 2 3 4 5 6]
myslice1 = [1 2 3 4 5 6 20 21]
4. Append One Slice to Another Slice #
You can append all elements of one slice to another using append(slice1, slice2...)
.
Example:
package main
import ("fmt")
func main() {
myslice1 := []int{1,2,3}
myslice2 := []int{4,5,6}
myslice3 := append(myslice1, myslice2...)
fmt.Printf("myslice3=%v\n", myslice3)
}
Result:
myslice3=[1 2 3 4 5 6]
5. Change the Length of a Slice #
Unlike arrays, the length of a slice can be changed by re-slicing or appending new elements.
Example:
package main
import ("fmt")
func main() {
arr1 := [6]int{9, 10, 11, 12, 13, 14}
myslice1 := arr1[1:5] // Initial slice
fmt.Printf("myslice1 = %v, length = %d, capacity = %d\n", myslice1, len(myslice1), cap(myslice1))
myslice1 = arr1[1:3] // Change length by re-slicing
fmt.Printf("myslice1 = %v, length = %d, capacity = %d\n", myslice1, len(myslice1), cap(myslice1))
myslice1 = append(myslice1, 20, 21, 22, 23) // Change length by appending
fmt.Printf("myslice1 = %v, length = %d, capacity = %d\n", myslice1, len(myslice1), cap(myslice1))
}
Result:
myslice1 = [10 11 12 13], length = 4, capacity = 5
myslice1 = [10 11], length = 2, capacity = 5
myslice1 = [10 11 20 21 22 23], length = 6, capacity = 10
6. Copy Slices for Memory Efficiency #
To reduce memory usage, you can copy only the required elements to a new slice using the copy()
function.
Syntax:
copy(dest, src)
Example:
package main
import ("fmt")
func main() {
numbers := []int{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
neededNumbers := numbers[:len(numbers)-10]
numbersCopy := make([]int, len(neededNumbers))
copy(numbersCopy, neededNumbers)
fmt.Printf("numbersCopy = %v, length = %d, capacity = %d\n", numbersCopy, len(numbersCopy), cap(numbersCopy))
}
Result:
// Original slice
numbers = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
// New slice
numbersCopy = [1 2 3 4 5], length = 5, capacity = 5
Explanation:
- The new slice has a smaller underlying array, reducing memory consumption.