~ubuntu-branches/ubuntu/trusty/golang/trusty

« back to all changes in this revision

Viewing changes to src/pkg/bytes/example_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
package bytes_test
6
6
 
7
7
import (
8
 
        . "bytes"
 
8
        "bytes"
9
9
        "encoding/base64"
 
10
        "fmt"
10
11
        "io"
11
12
        "os"
 
13
        "sort"
12
14
)
13
15
 
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!
20
22
}
21
23
 
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!
28
30
}
 
31
 
 
32
func ExampleCompare() {
 
33
        // Interpret Compare's result by comparing it to zero.
 
34
        var a, b []byte
 
35
        if bytes.Compare(a, b) < 0 {
 
36
                // a less b
 
37
        }
 
38
        if bytes.Compare(a, b) <= 0 {
 
39
                // a less or equal b
 
40
        }
 
41
        if bytes.Compare(a, b) > 0 {
 
42
                // a greater b
 
43
        }
 
44
        if bytes.Compare(a, b) >= 0 {
 
45
                // a greater or equal b
 
46
        }
 
47
 
 
48
        // Prefer Equal to Compare for equality comparisons.
 
49
        if bytes.Equal(a, b) {
 
50
                // a equal b
 
51
        }
 
52
        if !bytes.Equal(a, b) {
 
53
                // a not equal b
 
54
        }
 
55
}
 
56
 
 
57
func ExampleCompare_search() {
 
58
        // Binary search to find a matching byte slice.
 
59
        var needle []byte
 
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
 
64
        })
 
65
        if i < len(haystack) && bytes.Equal(haystack[i], needle) {
 
66
                // Found it!
 
67
        }
 
68
}
 
69
 
 
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!"))...)
 
75
        os.Stdout.Write(b)
 
76
        // Output: Hello, world!
 
77
}
 
78
 
 
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!
 
85
}