blob: 4a7cf33eceba189ba946c3345c962d3110f1409b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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")
}
}
|