GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage


Find looks through each value in the slice, returning the first one that passes a truth test (predicate), or the default value for the type and an error if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn’t traverse the entire slice.

package main

import (
	u "github.com/rjNemo/underscore"

func main() {
	nums := []int{2, 4, 5, 6, 8, 0}
	isOdd := func(n int) bool { return n%2 != 0 }

	n, err := u.Find(nums, isOdd)
	fmt.Println(n)   // 5
	fmt.Println(err) // nil