All Is True in Go

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

import (
    "fmt"
    "strings"
)

func All(vs []string, f func(string) bool) bool {
    for _, v := range vs {
        if !f(v) {
            return false
        }
    }
    return true
}

func main() {

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

    fmt.Println(All(strs, func(v string) bool {
        return strings.HasPrefix(v, "p")
    }))
}