Skip to content

Instantly share code, notes, and snippets.

@ik5
Created October 12, 2016 18:26
Show Gist options
  • Save ik5/e25c65c4e61428e29bc723123005a847 to your computer and use it in GitHub Desktop.
Save ik5/e25c65c4e61428e29bc723123005a847 to your computer and use it in GitHub Desktop.
searches for elements in a slice
package main
import "fmt"
func sliceIndex(limit int, predicate func(i int) bool) int {
for i := 0; i < limit; i++ {
if predicate(i) {
return i
}
}
return -1
}
func searchString(list []string, needle string) bool {
f := func(i int) bool {
return list[i] == needle
}
return sliceIndex(len(list), f) > -1
}
func main() {
list := []string{"development", "production"}
fmt.Println("found: ", searchString(list, "development"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment