/
example_test.go
77 lines (63 loc) · 1.34 KB
/
example_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package pgstream_test
import (
"bytes"
"database/sql"
"fmt"
"io"
"strings"
"github.com/ejholmes/pgstream"
_ "github.com/lib/pq"
)
const poem = `Let us take the river
path near Fall Hill.
There we will negotiate
an outcrop with its silvered
initials & other bits of graffiti,
all the way to the broken edge
that overlooks the bend,
& hold hands until
we can no longer tell
where the river ends.`
func Example() {
db := newDB()
stream := db.Stream("stream")
// Start writing the poem to stream in a separate goroutine.
go write(stream)
print(stream)
// Output:
// Let us take the river
// path near Fall Hill.
//
// There we will negotiate
// an outcrop with its silvered
// initials & other bits of graffiti,
//
// all the way to the broken edge
// that overlooks the bend,
// & hold hands until
//
// we can no longer tell
// where the river ends.
}
func write(w io.WriteCloser) error {
if _, err := io.Copy(w, strings.NewReader(poem)); err != nil {
return err
}
return w.Close()
}
func print(r io.Reader) error {
b := new(bytes.Buffer)
if _, err := io.Copy(b, r); err != nil {
return err
}
fmt.Print(b)
return nil
}
func newDB() *pgstream.DB {
db, err := sql.Open("postgres", "postgres://localhost/pgstream?sslmode=disable")
if err != nil {
panic(err)
}
db.Exec("TRUNCATE TABLE logs")
return pgstream.Open(db)
}