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.

51 lines
928 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, ch chan *Tweet) {
for {
tweet, err := stream.Next()
if err == ErrEOF {
close(ch)
}
ch <- tweet
}
}
func consumer(ch chan *Tweet) {
for t := range ch {
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()
ch := make(chan *Tweet)
// Producer
go producer(stream, ch)
// Consumer
go consumer(ch)
fmt.Printf("Process took %s\n", time.Since(start))
}