Retro68/gcc/libgo/go/bytes/example_test.go

86 lines
1.9 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bytes_test
import (
2014-09-21 17:33:12 +00:00
"bytes"
2012-03-27 23:13:14 +00:00
"encoding/base64"
2014-09-21 17:33:12 +00:00
"fmt"
2012-03-27 23:13:14 +00:00
"io"
"os"
2014-09-21 17:33:12 +00:00
"sort"
2012-03-27 23:13:14 +00:00
)
func ExampleBuffer() {
2014-09-21 17:33:12 +00:00
var b bytes.Buffer // A Buffer needs no initialization.
2012-03-27 23:13:14 +00:00
b.Write([]byte("Hello "))
2014-09-21 17:33:12 +00:00
fmt.Fprintf(&b, "world!")
2012-03-27 23:13:14 +00:00
b.WriteTo(os.Stdout)
// Output: Hello world!
}
func ExampleBuffer_reader() {
// A Buffer can turn a string or a []byte into an io.Reader.
2014-09-21 17:33:12 +00:00
buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==")
2012-03-27 23:13:14 +00:00
dec := base64.NewDecoder(base64.StdEncoding, buf)
io.Copy(os.Stdout, dec)
// Output: Gophers rule!
}
2014-09-21 17:33:12 +00:00
func ExampleCompare() {
// Interpret Compare's result by comparing it to zero.
var a, b []byte
if bytes.Compare(a, b) < 0 {
// a less b
}
if bytes.Compare(a, b) <= 0 {
// a less or equal b
}
if bytes.Compare(a, b) > 0 {
// a greater b
}
if bytes.Compare(a, b) >= 0 {
// a greater or equal b
}
// Prefer Equal to Compare for equality comparisons.
if bytes.Equal(a, b) {
// a equal b
}
if !bytes.Equal(a, b) {
// a not equal b
}
}
func ExampleCompare_search() {
// Binary search to find a matching byte slice.
var needle []byte
var haystack [][]byte // Assume sorted
i := sort.Search(len(haystack), func(i int) bool {
// Return haystack[i] >= needle.
return bytes.Compare(haystack[i], needle) >= 0
})
if i < len(haystack) && bytes.Equal(haystack[i], needle) {
// Found it!
}
}
func ExampleTrimSuffix() {
var b = []byte("Hello, goodbye, etc!")
b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
b = bytes.TrimSuffix(b, []byte("gopher"))
b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
os.Stdout.Write(b)
// Output: Hello, world!
}
func ExampleTrimPrefix() {
var b = []byte("Goodbye,, world!")
b = bytes.TrimPrefix(b, []byte("Goodbye,"))
b = bytes.TrimPrefix(b, []byte("See ya,"))
fmt.Printf("Hello%s", b)
// Output: Hello, world!
}