-
Notifications
You must be signed in to change notification settings - Fork 36
/
memconn_example_buffered_test.go
46 lines (34 loc) · 1.05 KB
/
memconn_example_buffered_test.go
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
44
45
46
package memconn_test
import (
"io"
"os"
"github.com/akutz/memconn"
)
// ExampleBuffered illustrates a server and client that
// communicate over a buffered, in-memory connection.
func Example_buffered() {
// Announce a new listener named "localhost" on MemConn's
// buffered network, "memb".
lis, _ := memconn.Listen("memb", "localhost")
// Ensure the listener is closed.
defer lis.Close()
// Start a goroutine that will wait for a client to dial the
// listener and then echo back any data sent to the remote
// connection.
go func() {
conn, _ := lis.Accept()
// If no errors occur then make sure the connection is closed.
defer conn.Close()
// Echo the data back to the client.
io.CopyN(conn, conn, 13)
}()
// Dial the buffered, in-memory network named "localhost".
conn, _ := memconn.Dial("memb", "localhost")
// Ensure the connection is closed.
defer conn.Close()
// Write the data to the server.
conn.Write([]byte("Hello, world."))
// Read the data from the server.
io.CopyN(os.Stdout, conn, 13)
// Output: Hello, world.
}