14
16
func ExampleBuffer() {
15
var b Buffer // A Buffer needs no initialization.
17
var b bytes.Buffer // A Buffer needs no initialization.
16
18
b.Write([]byte("Hello "))
17
b.Write([]byte("world!"))
19
fmt.Fprintf(&b, "world!")
18
20
b.WriteTo(os.Stdout)
19
21
// Output: Hello world!
22
24
func ExampleBuffer_reader() {
23
25
// A Buffer can turn a string or a []byte into an io.Reader.
24
buf := NewBufferString("R29waGVycyBydWxlIQ==")
26
buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==")
25
27
dec := base64.NewDecoder(base64.StdEncoding, buf)
26
28
io.Copy(os.Stdout, dec)
27
29
// Output: Gophers rule!
32
func ExampleCompare() {
33
// Interpret Compare's result by comparing it to zero.
35
if bytes.Compare(a, b) < 0 {
38
if bytes.Compare(a, b) <= 0 {
41
if bytes.Compare(a, b) > 0 {
44
if bytes.Compare(a, b) >= 0 {
45
// a greater or equal b
48
// Prefer Equal to Compare for equality comparisons.
49
if bytes.Equal(a, b) {
52
if !bytes.Equal(a, b) {
57
func ExampleCompare_search() {
58
// Binary search to find a matching byte slice.
60
var haystack [][]byte // Assume sorted
61
i := sort.Search(len(haystack), func(i int) bool {
62
// Return haystack[i] >= needle.
63
return bytes.Compare(haystack[i], needle) >= 0
65
if i < len(haystack) && bytes.Equal(haystack[i], needle) {
70
func ExampleTrimSuffix() {
71
var b = []byte("Hello, goodbye, etc!")
72
b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
73
b = bytes.TrimSuffix(b, []byte("gopher"))
74
b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
76
// Output: Hello, world!
79
func ExampleTrimPrefix() {
80
var b = []byte("Goodbye,, world!")
81
b = bytes.TrimPrefix(b, []byte("Goodbye,"))
82
b = bytes.TrimPrefix(b, []byte("See ya,"))
83
fmt.Printf("Hello%s", b)
84
// Output: Hello, world!