-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
60 lines (48 loc) · 829 Bytes
/
example_test.go
File metadata and controls
60 lines (48 loc) · 829 Bytes
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package kv_test
import (
"fmt"
"log"
"net"
"net/http"
"net/rpc"
"time"
"github.com/boltdb/bolt"
"github.com/helinwang/kv"
)
func Example() {
go func() {
// Start the service.
db, err := bolt.Open("/tmp/db_test.bin", 0666, nil)
if err != nil {
panic(err)
}
s := &kv.Service{DB: db}
rpc.Register(s)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":8081")
if e != nil {
log.Fatal("listen error:", e)
}
err = http.Serve(l, nil)
if err != nil {
panic(err)
}
}()
// Wait for the service to start.
time.Sleep(50 * time.Millisecond)
c, err := kv.New(":8081")
if err != nil {
panic(err)
}
err = c.Put([]byte("hello"), []byte("hi"))
if err != nil {
panic(err)
}
v, err := c.Get([]byte("hello"))
if err != nil {
panic(err)
}
fmt.Println(string(v))
// Output:
// hi
}