We often need our programs to perform operations on collections of data, like selecting all items that satisfy a given predicate or mapping all items to a new collection with a custom function. |
|
In some languages it’s idiomatic to use generic data structures and algorithms. Go does not support generics; in Go it’s common to provide collection functions if and when they are specifically needed for your program and data types. |
|
Here are some example collection functions for slices
of |
|
package main
|
|
import "strings"
import "fmt"
|
|
Returns the first index of the target string |
func Index(vs []string, t string) int {
for i, v := range vs {
if v == t {
return i
}
}
return -1
}
|
Returns |
func Include(vs []string, t string) bool {
return Index(vs, t) >= 0
}
|
Returns |
func Any(vs []string, f func(string) bool) bool {
for _, v := range vs {
if f(v) {
return true
}
}
return false
}
|
Returns |
func All(vs []string, f func(string) bool) bool {
for _, v := range vs {
if !f(v) {
return false
}
}
return true
}
|
Returns a new slice containing all strings in the
slice that satisfy the predicate |
func Filter(vs []string, f func(string) bool) []string {
vsf := make([]string, 0)
for _, v := range vs {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}
|
Returns a new slice containing the results of applying
the function |
func Map(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
|
func main() {
|
|
Here we try out our various collection functions. |
var strs = []string{"peach", "apple", "pear", "plum"}
|
fmt.Println(Index(strs, "pear"))
|
|
fmt.Println(Include(strs, "grape"))
|
|
fmt.Println(Any(strs, func(v string) bool {
return strings.HasPrefix(v, "p")
}))
|
|
fmt.Println(All(strs, func(v string) bool {
return strings.HasPrefix(v, "p")
}))
|
|
fmt.Println(Filter(strs, func(v string) bool {
return strings.Contains(v, "e")
}))
|
|
The above examples all used anonymous functions, but you can also use named functions of the correct type. |
fmt.Println(Map(strs, strings.ToUpper))
|
}
|
$ go run collection-functions.go
2
false
true
false
[peach apple pear]
[PEACH APPLE PEAR PLUM]
|
Previous example: Defer.
Next example: String Functions.