Created
October 12, 2016 18:26
-
-
Save ik5/e25c65c4e61428e29bc723123005a847 to your computer and use it in GitHub Desktop.
searches for elements in a slice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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