Last active
December 29, 2015 11:55
-
-
Save zaky/bd013f0a2eea71ed9856 to your computer and use it in GitHub Desktop.
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
| /* | |
| Find if one string out of a list exists in an array of strings | |
| */ | |
| package main | |
| import ( | |
| "fmt" | |
| "strings" | |
| "time" | |
| ) | |
| func main() { | |
| src := "I retired early because I realized that I would need about 20 years of full-time work to complete The Art of Computer Programming (TAOCP), which I have always viewed as the most important project of my life . " | |
| f, c := findIn(strings.Split(src, ` `)) | |
| go f(`work`) | |
| go f(`lXife`) | |
| FL: | |
| for { | |
| select { | |
| case b := <-c: | |
| if b { | |
| fmt.Println(`found`) | |
| break FL | |
| } else { | |
| fmt.Println(`didnt find`) | |
| } | |
| break | |
| case <-time.After(time.Second * 2): | |
| fmt.Println(`problem`) | |
| break | |
| } | |
| } | |
| println(`finished`) | |
| } | |
| func findIn(hay []string) (func(needle string), <-chan bool) { | |
| ret := make(chan bool) | |
| return func(needle string) { | |
| fmt.Println(needle) | |
| for _, val := range hay { | |
| if val == needle { | |
| ret <- true | |
| return | |
| } | |
| } | |
| ret <- false | |
| }, ret | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment