Skip to content

Instantly share code, notes, and snippets.

@sudaraka94
Created July 11, 2020 15:05
Show Gist options
  • Save sudaraka94/17a1fe9fa7d9d4055c860cf9aa6442ea to your computer and use it in GitHub Desktop.
Save sudaraka94/17a1fe9fa7d9d4055c860cf9aa6442ea to your computer and use it in GitHub Desktop.
Example of producer consumer concurrency pattern in Go
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