Go
Slice Operations
Common slice operations.
By EZ4Code Team
sliceslice
Code
package main
import (
"fmt"
"sort"
)
func main() {
var s []int
s = append(s, 1, 2, 3)
s = append(s, []int{4, 5}...)
// Slice
sub := s[1:4]
fmt.Println(sub)
// Copy
dst := make([]int, len(s))
copy(dst, s)
// Sort
sort.Ints(s)
fmt.Println(s)
// Find
idx := sort.SearchInts(s, 3)
fmt.Println(idx)
// Delete index 1
s = append(s[:1], s[2:]...)
}Explanation
Slices are dynamic arrays; append adds, copy duplicates, and slice tricks delete elements.