-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
64 lines (49 loc) · 1.01 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
package io2_test
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/jarxorg/io2"
)
func ExampleDelegateReader() {
org := bytes.NewReader([]byte(`original`))
r := io2.DelegateReader(org)
r.ReadFunc = func(p []byte) (int, error) {
return 0, errors.New("custom")
}
var err error
_, err = ioutil.ReadAll(r)
fmt.Printf("Error: %v\n", err)
// Output: Error: custom
}
func ExampleNewWriteSeekBuffer() {
o := io2.NewWriteSeekBuffer(0)
o.Write([]byte(`Hello!`))
o.Truncate(o.Len() - 1)
o.Write([]byte(` world!`))
fmt.Println(string(o.Bytes()))
o.Seek(-1, io.SeekEnd)
o.Write([]byte(`?`))
fmt.Println(string(o.Bytes()))
// Output:
// Hello world!
// Hello world?
}
func ExampleMultiReadSeeker() {
r, _ := io2.NewMultiReadSeeker(
strings.NewReader("Hello !"),
strings.NewReader(" World"),
)
r.Seek(5, io.SeekStart)
p, _ := ioutil.ReadAll(r)
fmt.Println(string(p))
r.Seek(-5, io.SeekEnd)
p, _ = ioutil.ReadAll(r)
fmt.Println(string(p))
// Output:
// ! World
// World
}