slices
package provides some utility functions on Go slice types. It leverages the generic types hence requires Go 1.18 and above.
You can simply run go get github.com/isacikgoz/slices
to start using in your own code.
Delete with preserving the order of the slice
s := []int{0, 1, 2, 3}
s = slices.Delete(s, 0) // removes the first element from the slice
Insert another slice from an index
s1 := []int{0, 3, 4, 5}
s2 := []int{1, 2}
s1 = slices.Insert(s1, 1, s2...) // will have (0, 1, 2, 3, 4, 5)
Filter slice
s1 := []string{
"http://foo.com",
"https://bar.com",
"https://example.net",
"http://go.org",
}
s2 := Filter(s1, func(v string) bool {
return strings.HasPrefix(v, "https://")
})
// s2 will be {"https://bar.com", "https://example.net"}