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.
17 func ExampleBuffer() {
18 var b bytes.Buffer // A Buffer needs no initialization.
19 b.Write([]byte("Hello "))
20 fmt.Fprintf(&b, "world!")
22 // Output: Hello world!
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!
33 func ExampleBuffer_Bytes() {
35 buf.Write([]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'})
36 os.Stdout.Write(buf.Bytes())
37 // Output: hello world
40 func ExampleBuffer_Cap() {
41 buf1 := bytes.NewBuffer(make([]byte, 10))
42 buf2 := bytes.NewBuffer(make([]byte, 0, 10))
43 fmt.Println(buf1.Cap())
44 fmt.Println(buf2.Cap())
50 func ExampleBuffer_Grow() {
54 b.Write([]byte("64 bytes or fewer"))
55 fmt.Printf("%q", bb[:b.Len()])
56 // Output: "64 bytes or fewer"
59 func ExampleBuffer_Len() {
62 b.Write([]byte("abcde"))
63 fmt.Printf("%d", b.Len())
67 func ExampleBuffer_Next() {
70 b.Write([]byte("abcde"))
71 fmt.Printf("%s\n", string(b.Next(2)))
72 fmt.Printf("%s\n", string(b.Next(2)))
73 fmt.Printf("%s", string(b.Next(2)))
80 func ExampleBuffer_Read() {
83 b.Write([]byte("abcde"))
84 rdbuf := make([]byte, 1)
85 n, err := b.Read(rdbuf)
90 fmt.Println(b.String())
91 fmt.Println(string(rdbuf))
98 func ExampleBuffer_ReadByte() {
101 b.Write([]byte("abcde"))
102 c, err := b.ReadByte()
107 fmt.Println(b.String())
113 func ExampleClone() {
115 clone := bytes.Clone(b)
116 fmt.Printf("%s\n", clone)
118 fmt.Printf("%s\n", b)
119 fmt.Printf("%s\n", clone)
126 func ExampleCompare() {
127 // Interpret Compare's result by comparing it to zero.
129 if bytes.Compare(a, b) < 0 {
132 if bytes.Compare(a, b) <= 0 {
135 if bytes.Compare(a, b) > 0 {
138 if bytes.Compare(a, b) >= 0 {
139 // a greater or equal b
142 // Prefer Equal to Compare for equality comparisons.
143 if bytes.Equal(a, b) {
146 if !bytes.Equal(a, b) {
151 func ExampleCompare_search() {
152 // Binary search to find a matching byte slice.
154 var haystack [][]byte // Assume sorted
155 i := sort.Search(len(haystack), func(i int) bool {
156 // Return haystack[i] >= needle.
157 return bytes.Compare(haystack[i], needle) >= 0
159 if i < len(haystack) && bytes.Equal(haystack[i], needle) {
164 func ExampleContains() {
165 fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo")))
166 fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar")))
167 fmt.Println(bytes.Contains([]byte("seafood"), []byte("")))
168 fmt.Println(bytes.Contains([]byte(""), []byte("")))
176 func ExampleContainsAny() {
177 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!"))
178 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的."))
179 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), ""))
180 fmt.Println(bytes.ContainsAny([]byte(""), ""))
188 func ExampleContainsRune() {
189 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
190 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
191 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大'))
192 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!'))
193 fmt.Println(bytes.ContainsRune([]byte(""), '@'))
202 func ExampleCount() {
203 fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
204 fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune
211 show := func(s, sep string) {
212 before, after, found := bytes.Cut([]byte(s), []byte(sep))
213 fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
218 show("Gopher", "Badger")
220 // Cut("Gopher", "Go") = "", "pher", true
221 // Cut("Gopher", "ph") = "Go", "er", true
222 // Cut("Gopher", "er") = "Goph", "", true
223 // Cut("Gopher", "Badger") = "Gopher", "", false
226 func ExampleCutPrefix() {
227 show := func(s, sep string) {
228 after, found := bytes.CutPrefix([]byte(s), []byte(sep))
229 fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found)
234 // CutPrefix("Gopher", "Go") = "pher", true
235 // CutPrefix("Gopher", "ph") = "Gopher", false
238 func ExampleCutSuffix() {
239 show := func(s, sep string) {
240 before, found := bytes.CutSuffix([]byte(s), []byte(sep))
241 fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, before, found)
246 // CutSuffix("Gopher", "Go") = "Gopher", false
247 // CutSuffix("Gopher", "er") = "Goph", true
250 func ExampleEqual() {
251 fmt.Println(bytes.Equal([]byte("Go"), []byte("Go")))
252 fmt.Println(bytes.Equal([]byte("Go"), []byte("C++")))
258 func ExampleEqualFold() {
259 fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
263 func ExampleFields() {
264 fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz ")))
265 // Output: Fields are: ["foo" "bar" "baz"]
268 func ExampleFieldsFunc() {
269 f := func(c rune) bool {
270 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
272 fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f))
273 // Output: Fields are: ["foo1" "bar2" "baz3"]
276 func ExampleHasPrefix() {
277 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go")))
278 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C")))
279 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("")))
286 func ExampleHasSuffix() {
287 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go")))
288 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O")))
289 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami")))
290 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("")))
298 func ExampleIndex() {
299 fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
300 fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
306 func ExampleIndexByte() {
307 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k')))
308 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g')))
314 func ExampleIndexFunc() {
315 f := func(c rune) bool {
316 return unicode.Is(unicode.Han, c)
318 fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f))
319 fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f))
325 func ExampleIndexAny() {
326 fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
327 fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
333 func ExampleIndexRune() {
334 fmt.Println(bytes.IndexRune([]byte("chicken"), 'k'))
335 fmt.Println(bytes.IndexRune([]byte("chicken"), 'd'))
342 s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
343 fmt.Printf("%s", bytes.Join(s, []byte(", ")))
344 // Output: foo, bar, baz
347 func ExampleLastIndex() {
348 fmt.Println(bytes.Index([]byte("go gopher"), []byte("go")))
349 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go")))
350 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent")))
357 func ExampleLastIndexAny() {
358 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp"))
359 fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大"))
360 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!."))
367 func ExampleLastIndexByte() {
368 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g')))
369 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r')))
370 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z')))
377 func ExampleLastIndexFunc() {
378 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter))
379 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct))
380 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber))
388 rot13 := func(r rune) rune {
390 case r >= 'A' && r <= 'Z':
391 return 'A' + (r-'A'+13)%26
392 case r >= 'a' && r <= 'z':
393 return 'a' + (r-'a'+13)%26
397 fmt.Printf("%s\n", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher...")))
399 // 'Gjnf oevyyvt naq gur fyvgul tbcure...
402 func ExampleReader_Len() {
403 fmt.Println(bytes.NewReader([]byte("Hi!")).Len())
404 fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len())
410 func ExampleRepeat() {
411 fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2))
415 func ExampleReplace() {
416 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
417 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
423 func ExampleReplaceAll() {
424 fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo")))
429 func ExampleRunes() {
430 rs := bytes.Runes([]byte("go gopher"))
431 for _, r := range rs {
432 fmt.Printf("%#U\n", r)
446 func ExampleSplit() {
447 fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
448 fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
449 fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte("")))
450 fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins")))
453 // ["" "man " "plan " "canal panama"]
454 // [" " "x" "y" "z" " "]
458 func ExampleSplitN() {
459 fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2))
460 z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0)
461 fmt.Printf("%q (nil = %v)\n", z, z == nil)
467 func ExampleSplitAfter() {
468 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(",")))
469 // Output: ["a," "b," "c"]
472 func ExampleSplitAfterN() {
473 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
474 // Output: ["a," "b,c"]
477 func ExampleTitle() {
478 fmt.Printf("%s", bytes.Title([]byte("her royal highness")))
479 // Output: Her Royal Highness
482 func ExampleToTitle() {
483 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises")))
484 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб")))
490 func ExampleToTitleSpecial() {
491 str := []byte("ahoj vývojári golang")
492 totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str)
493 fmt.Println("Original : " + string(str))
494 fmt.Println("ToTitle : " + string(totitle))
496 // Original : ahoj vývojári golang
497 // ToTitle : AHOJ VÝVOJÁRİ GOLANG
500 func ExampleToValidUTF8() {
501 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("abc"), []byte("\uFFFD")))
502 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("a\xffb\xC0\xAFc\xff"), []byte("")))
503 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("\xed\xa0\x80"), []byte("abc")))
511 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
512 // Output: ["Achtung! Achtung"]
515 func ExampleTrimFunc() {
516 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter)))
517 fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter)))
518 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct)))
519 fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
527 func ExampleTrimLeft() {
528 fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789")))
533 func ExampleTrimLeftFunc() {
534 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter)))
535 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct)))
536 fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
543 func ExampleTrimPrefix() {
544 var b = []byte("Goodbye,, world!")
545 b = bytes.TrimPrefix(b, []byte("Goodbye,"))
546 b = bytes.TrimPrefix(b, []byte("See ya,"))
547 fmt.Printf("Hello%s", b)
548 // Output: Hello, world!
551 func ExampleTrimSpace() {
552 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
553 // Output: a lone gopher
556 func ExampleTrimSuffix() {
557 var b = []byte("Hello, goodbye, etc!")
558 b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
559 b = bytes.TrimSuffix(b, []byte("gopher"))
560 b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
562 // Output: Hello, world!
565 func ExampleTrimRight() {
566 fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789")))
571 func ExampleTrimRightFunc() {
572 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter)))
573 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct)))
574 fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
581 func ExampleToLower() {
582 fmt.Printf("%s", bytes.ToLower([]byte("Gopher")))
586 func ExampleToLowerSpecial() {
587 str := []byte("AHOJ VÝVOJÁRİ GOLANG")
588 totitle := bytes.ToLowerSpecial(unicode.AzeriCase, str)
589 fmt.Println("Original : " + string(str))
590 fmt.Println("ToLower : " + string(totitle))
592 // Original : AHOJ VÝVOJÁRİ GOLANG
593 // ToLower : ahoj vývojári golang
596 func ExampleToUpper() {
597 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
601 func ExampleToUpperSpecial() {
602 str := []byte("ahoj vývojári golang")
603 totitle := bytes.ToUpperSpecial(unicode.AzeriCase, str)
604 fmt.Println("Original : " + string(str))
605 fmt.Println("ToUpper : " + string(totitle))
607 // Original : ahoj vývojári golang
608 // ToUpper : AHOJ VÝVOJÁRİ GOLANG