forked from mauricioabreu/golings
-
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.
- Loading branch information
1 parent
749202c
commit dac11a2
Showing
2 changed files
with
55 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// concurrent3 | ||
// Make the tests pass! | ||
|
||
// I AM NOT DONE | ||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestSendAndReceive(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
messages := make(chan string) | ||
sendAndReceive(&buf, messages) | ||
|
||
got := buf.String() | ||
want := "Hello World" | ||
|
||
if got != want { | ||
t.Errorf("got %q want %q", got, want) | ||
} | ||
} | ||
|
||
func sendAndReceive(buf *bytes.Buffer, messages chan string) { | ||
go func() { | ||
messages <- "Hello" | ||
messages <- "World" | ||
close(messages) | ||
}() | ||
|
||
greeting := <-messages | ||
fmt.Fprint(buf, greeting) | ||
|
||
// Here we just receive the first message | ||
// Consider using a for-range loop to iterate over the messages | ||
_, ok := <-messages | ||
if !ok { | ||
fmt.Fprint(buf, "Channel is closed") | ||
} | ||
} |
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