github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/bytes/example_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package bytes_test 6 7 import ( 8 "bytes" 9 "encoding/base64" 10 "fmt" 11 "io" 12 "os" 13 "sort" 14 "unicode" 15 ) 16 17 func ExampleBuffer() { 18 var b bytes.Buffer // A Buffer needs no initialization. 19 b.Write([]byte("Hello ")) 20 fmt.Fprintf(&b, "world!") 21 b.WriteTo(os.Stdout) 22 // Output: Hello world! 23 } 24 25 func ExampleBuffer_reader() { 26 // A Buffer can turn a string or a []byte into an io.Reader. 27 buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==") 28 dec := base64.NewDecoder(base64.StdEncoding, buf) 29 io.Copy(os.Stdout, dec) 30 // Output: Gophers rule! 31 } 32 33 func ExampleBuffer_Grow() { 34 var b bytes.Buffer 35 b.Grow(64) 36 bb := b.Bytes() 37 b.Write([]byte("64 bytes or fewer")) 38 fmt.Printf("%q", bb[:b.Len()]) 39 // Output: "64 bytes or fewer" 40 } 41 42 func ExampleCompare() { 43 // Interpret Compare's result by comparing it to zero. 44 var a, b []byte 45 if bytes.Compare(a, b) < 0 { 46 // a less b 47 } 48 if bytes.Compare(a, b) <= 0 { 49 // a less or equal b 50 } 51 if bytes.Compare(a, b) > 0 { 52 // a greater b 53 } 54 if bytes.Compare(a, b) >= 0 { 55 // a greater or equal b 56 } 57 58 // Prefer Equal to Compare for equality comparisons. 59 if bytes.Equal(a, b) { 60 // a equal b 61 } 62 if !bytes.Equal(a, b) { 63 // a not equal b 64 } 65 } 66 67 func ExampleCompare_search() { 68 // Binary search to find a matching byte slice. 69 var needle []byte 70 var haystack [][]byte // Assume sorted 71 i := sort.Search(len(haystack), func(i int) bool { 72 // Return haystack[i] >= needle. 73 return bytes.Compare(haystack[i], needle) >= 0 74 }) 75 if i < len(haystack) && bytes.Equal(haystack[i], needle) { 76 // Found it! 77 } 78 } 79 80 func ExampleTrimSuffix() { 81 var b = []byte("Hello, goodbye, etc!") 82 b = bytes.TrimSuffix(b, []byte("goodbye, etc!")) 83 b = bytes.TrimSuffix(b, []byte("gopher")) 84 b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...) 85 os.Stdout.Write(b) 86 // Output: Hello, world! 87 } 88 89 func ExampleTrimPrefix() { 90 var b = []byte("Goodbye,, world!") 91 b = bytes.TrimPrefix(b, []byte("Goodbye,")) 92 b = bytes.TrimPrefix(b, []byte("See ya,")) 93 fmt.Printf("Hello%s", b) 94 // Output: Hello, world! 95 } 96 97 func ExampleFields() { 98 fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz "))) 99 // Output: Fields are: ["foo" "bar" "baz"] 100 } 101 102 func ExampleFieldsFunc() { 103 f := func(c rune) bool { 104 return !unicode.IsLetter(c) && !unicode.IsNumber(c) 105 } 106 fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f)) 107 // Output: Fields are: ["foo1" "bar2" "baz3"] 108 } 109 110 func ExampleContains() { 111 fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo"))) 112 fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar"))) 113 fmt.Println(bytes.Contains([]byte("seafood"), []byte(""))) 114 fmt.Println(bytes.Contains([]byte(""), []byte(""))) 115 // Output: 116 // true 117 // false 118 // true 119 // true 120 } 121 122 func ExampleCount() { 123 fmt.Println(bytes.Count([]byte("cheese"), []byte("e"))) 124 fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune 125 // Output: 126 // 3 127 // 5 128 } 129 130 func ExampleEqualFold() { 131 fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go"))) 132 // Output: true 133 } 134 135 func ExampleHasPrefix() { 136 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go"))) 137 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C"))) 138 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte(""))) 139 // Output: 140 // true 141 // false 142 // true 143 } 144 145 func ExampleHasSuffix() { 146 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go"))) 147 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O"))) 148 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami"))) 149 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte(""))) 150 // Output: 151 // true 152 // false 153 // false 154 // true 155 } 156 157 func ExampleIndex() { 158 fmt.Println(bytes.Index([]byte("chicken"), []byte("ken"))) 159 fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr"))) 160 // Output: 161 // 4 162 // -1 163 } 164 165 func ExampleIndexFunc() { 166 f := func(c rune) bool { 167 return unicode.Is(unicode.Han, c) 168 } 169 fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f)) 170 fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f)) 171 // Output: 172 // 7 173 // -1 174 } 175 176 func ExampleIndexAny() { 177 fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy")) 178 fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy")) 179 // Output: 180 // 2 181 // -1 182 } 183 184 func ExampleIndexRune() { 185 fmt.Println(bytes.IndexRune([]byte("chicken"), 'k')) 186 fmt.Println(bytes.IndexRune([]byte("chicken"), 'd')) 187 // Output: 188 // 4 189 // -1 190 } 191 192 func ExampleLastIndex() { 193 fmt.Println(bytes.Index([]byte("go gopher"), []byte("go"))) 194 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go"))) 195 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent"))) 196 // Output: 197 // 0 198 // 3 199 // -1 200 } 201 202 func ExampleJoin() { 203 s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")} 204 fmt.Printf("%s", bytes.Join(s, []byte(", "))) 205 // Output: foo, bar, baz 206 } 207 208 func ExampleRepeat() { 209 fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2)) 210 // Output: banana 211 } 212 213 func ExampleReplace() { 214 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2)) 215 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1)) 216 // Output: 217 // oinky oinky oink 218 // moo moo moo 219 } 220 221 func ExampleSplit() { 222 fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(","))) 223 fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a "))) 224 fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte(""))) 225 fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins"))) 226 // Output: 227 // ["a" "b" "c"] 228 // ["" "man " "plan " "canal panama"] 229 // [" " "x" "y" "z" " "] 230 // [""] 231 } 232 233 func ExampleSplitN() { 234 fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2)) 235 z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0) 236 fmt.Printf("%q (nil = %v)\n", z, z == nil) 237 // Output: 238 // ["a" "b,c"] 239 // [] (nil = true) 240 } 241 242 func ExampleSplitAfter() { 243 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(","))) 244 // Output: ["a," "b," "c"] 245 } 246 247 func ExampleSplitAfterN() { 248 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2)) 249 // Output: ["a," "b,c"] 250 } 251 252 func ExampleTitle() { 253 fmt.Printf("%s", bytes.Title([]byte("her royal highness"))) 254 // Output: Her Royal Highness 255 } 256 257 func ExampleToTitle() { 258 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises"))) 259 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб"))) 260 // Output: 261 // LOUD NOISES 262 // ХЛЕБ 263 } 264 265 func ExampleTrim() { 266 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! ")) 267 // Output: ["Achtung! Achtung"] 268 } 269 270 func ExampleMap() { 271 rot13 := func(r rune) rune { 272 switch { 273 case r >= 'A' && r <= 'Z': 274 return 'A' + (r-'A'+13)%26 275 case r >= 'a' && r <= 'z': 276 return 'a' + (r-'a'+13)%26 277 } 278 return r 279 } 280 fmt.Printf("%s", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher..."))) 281 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... 282 } 283 284 func ExampleTrimSpace() { 285 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n"))) 286 // Output: a lone gopher 287 } 288 289 func ExampleToUpper() { 290 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher"))) 291 // Output: GOPHER 292 } 293 294 func ExampleToLower() { 295 fmt.Printf("%s", bytes.ToLower([]byte("Gopher"))) 296 // Output: gopher 297 }