Find by Value

Find a value in a list by applying a predicate on each value.

Examples Filter
package main

import (
	"fmt"
	"strings"
)

func FindStr(list []string, pred func(s string) bool) string {
	for _, s := range list {
		if pred(s) {
			return s
		}
	}
	return ""
}

func main() {

	list := []string{"slice", "of", "strings"}
	res := FindStr(list, func(s string) bool {
		return strings.HasPrefix(s, "o")
	})
	fmt.Println(res)
}
Last Run  :
of