-
Notifications
You must be signed in to change notification settings - Fork 36
/
memconn_example_unbuffered_test.go
46 lines (34 loc) · 1.06 KB
/
memconn_example_unbuffered_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"
)
// ExampleUnbuffered illustrates a server and client that
// communicate over an unbuffered, in-memory connection.
func Example_unbuffered() {
// Announce a new listener named "localhost" on MemConn's
// unbuffered network, "memu".
lis, _ := memconn.Listen("memu", "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 unbuffered, in-memory network named "localhost".
conn, _ := memconn.Dial("memu", "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.
}