memio - example_test.go
1 package memio_test
2
3 import (
4 "errors"
5 "fmt"
6 "io"
7
8 "vimagination.zapto.org/memio"
9 )
10
11 // ExampleBuffer demonstrates writing and reading using Buffer.
12 func ExampleBuffer() {
13 var buf memio.Buffer
14
15 buf.WriteString("Hello, world!")
16
17 data := make([]byte, 5)
18
19 buf.Read(data)
20
21 fmt.Printf("%s", data)
22 // Output: Hello
23 }
24
25 // ExampleLimitedBuffer shows how LimitedBuffer prevents writes beyond capacity.
26 func ExampleLimitedBuffer() {
27 // Preallocate 5 bytes
28 data := make([]byte, 5)
29 lb := memio.LimitedBuffer(data)
30
31 // Write exactly 5 bytes
32 lb.WriteString("12345")
33
34 // Attempting to write more will not extend capacity
35 n, err := lb.WriteString("678")
36 fmt.Println(n, errors.Is(err, io.ErrShortBuffer))
37 // Output: 0 true
38 }
39