Filter By Value in Go

Posted by yhuang
Public (Editable by Users)
Go
Edit
package main

import (
    "fmt"
    "strings"
)

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
}

func main() {

    var strs = []string{"peach", "apple", "pear", "plum"}

    fmt.Println(Filter(strs, func(v string) bool {
        return strings.Contains(v, "e")
    }))
}