You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
939 B
Go

//////////////////////////////////////////////////////////////////////
//
// Given is a producer-consumer scenario, where a producer reads in
// tweets from a mockstream and a consumer is processing the
// data. Your task is to change the code so that the producer as well
// as the consumer can run concurrently
//
package main
import (
"fmt"
"time"
)
func producer(stream Stream) (tweets []*Tweet) {
for {
tweet, err := stream.Next()
if err == ErrEOF {
return tweets
}
tweets = append(tweets, tweet)
}
}
func consumer(tweets []*Tweet) {
for _, t := range tweets {
if t.IsTalkingAboutGo() {
fmt.Println(t.Username, "\ttweets about golang")
} else {
fmt.Println(t.Username, "\tdoes not tweet about golang")
}
}
}
func main() {
start := time.Now()
stream := GetMockStream()
// Producer
tweets := producer(stream)
// Consumer
consumer(tweets)
fmt.Printf("Process took %s\n", time.Since(start))
}