forked from loong/go-concurrency-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solution: simplify solution by just closing the tweets channel
- Loading branch information
1 parent
94541b6
commit 8d9c332
Showing
2 changed files
with
93 additions
and
85 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
////////////////////////////////////////////////////////////////////// | ||
// | ||
// DO NOT EDIT THIS PART | ||
// Your task is to edit `main.go` | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// GetMockStream is a blackbox function which returns a mock stream for | ||
// demonstration purposes | ||
func GetMockStream() Stream { | ||
return Stream{0, mockdata} | ||
} | ||
|
||
// Stream is a mock stream for demonstration purposes, not threadsafe | ||
type Stream struct { | ||
pos int | ||
tweets []Tweet | ||
} | ||
|
||
// ErrEOF returns on End of File error | ||
var ErrEOF = errors.New("End of File") | ||
|
||
// Next returns the next Tweet in the stream, returns EOF error if | ||
// there are no more tweets | ||
func (s *Stream) Next() (*Tweet, error) { | ||
|
||
// simulate delay | ||
time.Sleep(320 * time.Millisecond) | ||
if s.pos >= len(s.tweets) { | ||
return &Tweet{}, ErrEOF | ||
} | ||
|
||
tweet := s.tweets[s.pos] | ||
s.pos++ | ||
|
||
return &tweet, nil | ||
} | ||
|
||
// Tweet defines the simlified representation of a tweet | ||
type Tweet struct { | ||
Username string | ||
Text string | ||
} | ||
|
||
// IsTalkingAboutGo is a mock process which pretend to be a sophisticated procedure to analyse whether tweet is talking about go or not | ||
func (t *Tweet) IsTalkingAboutGo() bool { | ||
// simulate delay | ||
time.Sleep(330 * time.Millisecond) | ||
|
||
hasGolang := strings.Contains(strings.ToLower(t.Text), "golang") | ||
hasGopher := strings.Contains(strings.ToLower(t.Text), "gopher") | ||
|
||
return hasGolang || hasGopher | ||
} | ||
|
||
var mockdata = []Tweet{ | ||
{ | ||
"davecheney", | ||
"#golang top tip: if your unit tests import any other package you wrote, including themselves, they're not unit tests.", | ||
}, { | ||
"beertocode", | ||
"Backend developer, doing frontend featuring the eternal struggle of centering something. #coding", | ||
}, { | ||
"ironzeb", | ||
"Re: Popularity of Golang in China: My thinking nowadays is that it had a lot to do with this book and author https://github.com/astaxie/build-web-application-with-golang", | ||
}, { | ||
"beertocode", | ||
"Looking forward to the #gopher meetup in Hsinchu tonight with @ironzeb!", | ||
}, { | ||
"vampirewalk666", | ||
"I just wrote a golang slack bot! It reports the state of github repository. #Slack #golang", | ||
}, | ||
} |