Go filter and map
last modified February 18, 2022
Go filter/map tutorial shows how to create and use filter and map functions in Golang.
A filter function processes a collection and produces a new collection containing exactly those elements for which the given predicate returns true.
A map function applies a given function to each element of a collection, returning the results in a new collection.
A predicate is a single-argument function which returns a boolean value.
Go filter example
In the next example, we apply the filter function on a slice of strings.
package main import ( "fmt" "strings" ) func filter(data []string, f func(string) bool) []string { fltd := make([]string, 0) for _, e := range data { if f(e) { fltd = append(fltd, e) } } return fltd } func main() { words := []string{"war", "water", "cup", "tree", "storm"} p := "w" res := filter(words, func(s string) bool { return strings.HasPrefix(s, p) }) fmt.Println(res) }
We have a slice of words. With the help of the filter
function, we
filter out all words that start with 'w' and put them into a new slice.
func filter(data []string, f func(string) bool) []string { fltd := make([]string, 0) for _, e := range data { if f(e) { fltd = append(fltd, e) } } return fltd }
The filter
function takes a slice of words and the predicate as
parameters. The predicate is a function that is applied on every slice element.
$ go run main.go [war water]
In the next example, we filter a slice of struct types.
package main import "fmt" type User struct { name string occupation string country string } func filter(data []User, f func(User) bool) []User { fltd := make([]User, 0) for _, user := range data { if f(user) { fltd = append(fltd, user) } } return fltd } func main() { users := []User{ {"John Doe", "gardener", "USA"}, {"Roger Roe", "driver", "UK"}, {"Paul Smith", "programmer", "Canada"}, {"Lucia Mala", "teacher", "Slovakia"}, {"Patrick Connor", "shopkeeper", "USA"}, {"Tim Welson", "programmer", "Canada"}, {"Tomas Smutny", "programmer", "Slovakia"}, } country := "Slovakia" res := filter(users, func(u User) bool { return u.country == country }) fmt.Println(res) }
The example filters all users that live in Slovakia.
$ go run main.go [{Lucia Mala teacher Slovakia} {Tomas Smutny programmer Slovakia}]
Go map example
In the following example, we transform a slice of integers. Note that we cannot name the function map, since it is a Go keyword for creating dictionaries/maps.
package main import ( "fmt" ) func map2(data []int, f func(int) int) []int { mapped := make([]int, len(data)) for i, e := range data { mapped[i] = f(e) } return mapped } func main() { vals := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} res := map2(vals, func(i int) int { return i * i }) fmt.Println(res) }
We have a slice of integers. We square all elements of the slice.
$ go run main.go [1 4 9 16 25 36 49 64 81 100]
In the next example, we apply a map function on a slice of strings.
package main import ( "fmt" "strings" ) func map2(data []string, f func(string) string) []string { mapped := make([]string, len(data)) for i, e := range data { mapped[i] = f(e) } return mapped } func main() { words := []string{"war", "water", "cup", "tree", "storm"} res := map2(words, strings.ToUpper) fmt.Println(res) }
The example transforms a slice of strings into uppercase.
$ go run main.go [WAR WATER CUP TREE STORM]
In this tutorial, we have worked with filter and map functions in Golang.
List all Go tutorials.