forked from agtorre/go-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes.go
45 lines (36 loc) · 1.03 KB
/
bytes.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
package bytestrings
import (
"bufio"
"bytes"
"fmt"
)
// WorkWithBuffer will make use of the buffer created by the
// Buffer function
func WorkWithBuffer() error {
rawString := "it's easy to encode unicode into a byte array ❤️"
b := Buffer(rawString)
// we can quickly convert a buffer back into byes with
// b.Bytes() or a string with b.String()
fmt.Println(b.String())
// because this is an io Reader we can make use of generic
// io reader functions such as
s, err := toString(b)
if err != nil {
return err
}
fmt.Println(s)
// we can also take our bytes and create a bytes reader
// these readers implement io.Reader, io.ReaderAt,
// io.WriterTo, io.Seeker, io.ByteScanner, and io.RuneScanner
// interfaces
reader := bytes.NewReader([]byte(rawString))
// we can also plug it into a scanner that allows buffered
// reading and tokenzation
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanWords)
// iterate over all of the scan events
for scanner.Scan() {
fmt.Print(scanner.Text())
}
return nil
}