Solved 1-producer-consumer

main
oabrivard 2 years ago
parent 5ef5f591f0
commit a4efadbe8a

@ -13,19 +13,19 @@ import (
"time" "time"
) )
func producer(stream Stream) (tweets []*Tweet) { func producer(stream Stream, ch chan *Tweet) {
for { for {
tweet, err := stream.Next() tweet, err := stream.Next()
if err == ErrEOF { if err == ErrEOF {
return tweets close(ch)
} }
tweets = append(tweets, tweet) ch <- tweet
} }
} }
func consumer(tweets []*Tweet) { func consumer(ch chan *Tweet) {
for _, t := range tweets { for t := range ch {
if t.IsTalkingAboutGo() { if t.IsTalkingAboutGo() {
fmt.Println(t.Username, "\ttweets about golang") fmt.Println(t.Username, "\ttweets about golang")
} else { } else {
@ -38,11 +38,13 @@ func main() {
start := time.Now() start := time.Now()
stream := GetMockStream() stream := GetMockStream()
ch := make(chan *Tweet)
// Producer // Producer
tweets := producer(stream) go producer(stream, ch)
// Consumer // Consumer
consumer(tweets) go consumer(ch)
fmt.Printf("Process took %s\n", time.Since(start)) fmt.Printf("Process took %s\n", time.Since(start))
} }

Loading…
Cancel
Save