Created
July 11, 2020 15:05
-
-
Save sudaraka94/17a1fe9fa7d9d4055c860cf9aa6442ea to your computer and use it in GitHub Desktop.
Example of producer consumer concurrency pattern in Go
This file contains 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" | |
"strings" | |
) | |
var users = map[int]string{ | |
1: "Rob", | |
2: "Ken", | |
3: "Robert", | |
} | |
func Search(substring string) <-chan string { | |
c := make(chan string) | |
go func() { | |
defer close(c) | |
for _, name := range users { | |
if ok := strings.Contains(name, substring); !ok { | |
continue | |
} | |
c <- name | |
} | |
}() | |
return c | |
} | |
func main() { | |
for name := range Search("Rob") { | |
fmt.Println(name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment