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 ExampleCompare() {
114 // Interpret Compare's result by comparing it to zero.
116 if bytes
.Compare(a
, b
) < 0 {
119 if bytes
.Compare(a
, b
) <= 0 {
122 if bytes
.Compare(a
, b
) > 0 {
125 if bytes
.Compare(a
, b
) >= 0 {
126 // a greater or equal b
129 // Prefer Equal to Compare for equality comparisons.
130 if bytes
.Equal(a
, b
) {
133 if !bytes
.Equal(a
, b
) {
138 func ExampleCompare_search() {
139 // Binary search to find a matching byte slice.
141 var haystack
[][]byte // Assume sorted
142 i
:= sort
.Search(len(haystack
), func(i
int) bool {
143 // Return haystack[i] >= needle.
144 return bytes
.Compare(haystack
[i
], needle
) >= 0
146 if i
< len(haystack
) && bytes
.Equal(haystack
[i
], needle
) {
151 func ExampleContains() {
152 fmt
.Println(bytes
.Contains([]byte("seafood"), []byte("foo")))
153 fmt
.Println(bytes
.Contains([]byte("seafood"), []byte("bar")))
154 fmt
.Println(bytes
.Contains([]byte("seafood"), []byte("")))
155 fmt
.Println(bytes
.Contains([]byte(""), []byte("")))
163 func ExampleContainsAny() {
164 fmt
.Println(bytes
.ContainsAny([]byte("I like seafood."), "fÄo!"))
165 fmt
.Println(bytes
.ContainsAny([]byte("I like seafood."), "去是伟大的."))
166 fmt
.Println(bytes
.ContainsAny([]byte("I like seafood."), ""))
167 fmt
.Println(bytes
.ContainsAny([]byte(""), ""))
175 func ExampleContainsRune() {
176 fmt
.Println(bytes
.ContainsRune([]byte("I like seafood."), 'f'))
177 fmt
.Println(bytes
.ContainsRune([]byte("I like seafood."), 'ö'))
178 fmt
.Println(bytes
.ContainsRune([]byte("去是伟大的!"), '大'))
179 fmt
.Println(bytes
.ContainsRune([]byte("去是伟大的!"), '!'))
180 fmt
.Println(bytes
.ContainsRune([]byte(""), '@'))
189 func ExampleCount() {
190 fmt
.Println(bytes
.Count([]byte("cheese"), []byte("e")))
191 fmt
.Println(bytes
.Count([]byte("five"), []byte(""))) // before & after each rune
198 show
:= func(s
, sep
string) {
199 before
, after
, found
:= bytes
.Cut([]byte(s
), []byte(sep
))
200 fmt
.Printf("Cut(%q, %q) = %q, %q, %v\n", s
, sep
, before
, after
, found
)
205 show("Gopher", "Badger")
207 // Cut("Gopher", "Go") = "", "pher", true
208 // Cut("Gopher", "ph") = "Go", "er", true
209 // Cut("Gopher", "er") = "Goph", "", true
210 // Cut("Gopher", "Badger") = "Gopher", "", false
213 func ExampleEqual() {
214 fmt
.Println(bytes
.Equal([]byte("Go"), []byte("Go")))
215 fmt
.Println(bytes
.Equal([]byte("Go"), []byte("C++")))
221 func ExampleEqualFold() {
222 fmt
.Println(bytes
.EqualFold([]byte("Go"), []byte("go")))
226 func ExampleFields() {
227 fmt
.Printf("Fields are: %q", bytes
.Fields([]byte(" foo bar baz ")))
228 // Output: Fields are: ["foo" "bar" "baz"]
231 func ExampleFieldsFunc() {
232 f
:= func(c rune
) bool {
233 return !unicode
.IsLetter(c
) && !unicode
.IsNumber(c
)
235 fmt
.Printf("Fields are: %q", bytes
.FieldsFunc([]byte(" foo1;bar2,baz3..."), f
))
236 // Output: Fields are: ["foo1" "bar2" "baz3"]
239 func ExampleHasPrefix() {
240 fmt
.Println(bytes
.HasPrefix([]byte("Gopher"), []byte("Go")))
241 fmt
.Println(bytes
.HasPrefix([]byte("Gopher"), []byte("C")))
242 fmt
.Println(bytes
.HasPrefix([]byte("Gopher"), []byte("")))
249 func ExampleHasSuffix() {
250 fmt
.Println(bytes
.HasSuffix([]byte("Amigo"), []byte("go")))
251 fmt
.Println(bytes
.HasSuffix([]byte("Amigo"), []byte("O")))
252 fmt
.Println(bytes
.HasSuffix([]byte("Amigo"), []byte("Ami")))
253 fmt
.Println(bytes
.HasSuffix([]byte("Amigo"), []byte("")))
261 func ExampleIndex() {
262 fmt
.Println(bytes
.Index([]byte("chicken"), []byte("ken")))
263 fmt
.Println(bytes
.Index([]byte("chicken"), []byte("dmr")))
269 func ExampleIndexByte() {
270 fmt
.Println(bytes
.IndexByte([]byte("chicken"), byte('k')))
271 fmt
.Println(bytes
.IndexByte([]byte("chicken"), byte('g')))
277 func ExampleIndexFunc() {
278 f
:= func(c rune
) bool {
279 return unicode
.Is(unicode
.Han
, c
)
281 fmt
.Println(bytes
.IndexFunc([]byte("Hello, 世界"), f
))
282 fmt
.Println(bytes
.IndexFunc([]byte("Hello, world"), f
))
288 func ExampleIndexAny() {
289 fmt
.Println(bytes
.IndexAny([]byte("chicken"), "aeiouy"))
290 fmt
.Println(bytes
.IndexAny([]byte("crwth"), "aeiouy"))
296 func ExampleIndexRune() {
297 fmt
.Println(bytes
.IndexRune([]byte("chicken"), 'k'))
298 fmt
.Println(bytes
.IndexRune([]byte("chicken"), 'd'))
305 s
:= [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
306 fmt
.Printf("%s", bytes
.Join(s
, []byte(", ")))
307 // Output: foo, bar, baz
310 func ExampleLastIndex() {
311 fmt
.Println(bytes
.Index([]byte("go gopher"), []byte("go")))
312 fmt
.Println(bytes
.LastIndex([]byte("go gopher"), []byte("go")))
313 fmt
.Println(bytes
.LastIndex([]byte("go gopher"), []byte("rodent")))
320 func ExampleLastIndexAny() {
321 fmt
.Println(bytes
.LastIndexAny([]byte("go gopher"), "MüQp"))
322 fmt
.Println(bytes
.LastIndexAny([]byte("go 地鼠"), "地大"))
323 fmt
.Println(bytes
.LastIndexAny([]byte("go gopher"), "z,!."))
330 func ExampleLastIndexByte() {
331 fmt
.Println(bytes
.LastIndexByte([]byte("go gopher"), byte('g')))
332 fmt
.Println(bytes
.LastIndexByte([]byte("go gopher"), byte('r')))
333 fmt
.Println(bytes
.LastIndexByte([]byte("go gopher"), byte('z')))
340 func ExampleLastIndexFunc() {
341 fmt
.Println(bytes
.LastIndexFunc([]byte("go gopher!"), unicode
.IsLetter
))
342 fmt
.Println(bytes
.LastIndexFunc([]byte("go gopher!"), unicode
.IsPunct
))
343 fmt
.Println(bytes
.LastIndexFunc([]byte("go gopher!"), unicode
.IsNumber
))
350 func ExampleReader_Len() {
351 fmt
.Println(bytes
.NewReader([]byte("Hi!")).Len())
352 fmt
.Println(bytes
.NewReader([]byte("こんにちは!")).Len())
358 func ExampleRepeat() {
359 fmt
.Printf("ba%s", bytes
.Repeat([]byte("na"), 2))
363 func ExampleReplace() {
364 fmt
.Printf("%s\n", bytes
.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
365 fmt
.Printf("%s\n", bytes
.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
371 func ExampleReplaceAll() {
372 fmt
.Printf("%s\n", bytes
.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo")))
377 func ExampleRunes() {
378 rs
:= bytes
.Runes([]byte("go gopher"))
379 for _
, r
:= range rs
{
380 fmt
.Printf("%#U\n", r
)
394 func ExampleSplit() {
395 fmt
.Printf("%q\n", bytes
.Split([]byte("a,b,c"), []byte(",")))
396 fmt
.Printf("%q\n", bytes
.Split([]byte("a man a plan a canal panama"), []byte("a ")))
397 fmt
.Printf("%q\n", bytes
.Split([]byte(" xyz "), []byte("")))
398 fmt
.Printf("%q\n", bytes
.Split([]byte(""), []byte("Bernardo O'Higgins")))
401 // ["" "man " "plan " "canal panama"]
402 // [" " "x" "y" "z" " "]
406 func ExampleSplitN() {
407 fmt
.Printf("%q\n", bytes
.SplitN([]byte("a,b,c"), []byte(","), 2))
408 z
:= bytes
.SplitN([]byte("a,b,c"), []byte(","), 0)
409 fmt
.Printf("%q (nil = %v)\n", z
, z
== nil)
415 func ExampleSplitAfter() {
416 fmt
.Printf("%q\n", bytes
.SplitAfter([]byte("a,b,c"), []byte(",")))
417 // Output: ["a," "b," "c"]
420 func ExampleSplitAfterN() {
421 fmt
.Printf("%q\n", bytes
.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
422 // Output: ["a," "b,c"]
425 func ExampleTitle() {
426 fmt
.Printf("%s", bytes
.Title([]byte("her royal highness")))
427 // Output: Her Royal Highness
430 func ExampleToTitle() {
431 fmt
.Printf("%s\n", bytes
.ToTitle([]byte("loud noises")))
432 fmt
.Printf("%s\n", bytes
.ToTitle([]byte("хлеб")))
438 func ExampleToTitleSpecial() {
439 str
:= []byte("ahoj vývojári golang")
440 totitle
:= bytes
.ToTitleSpecial(unicode
.AzeriCase
, str
)
441 fmt
.Println("Original : " + string(str
))
442 fmt
.Println("ToTitle : " + string(totitle
))
444 // Original : ahoj vývojári golang
445 // ToTitle : AHOJ VÝVOJÁRİ GOLANG
449 fmt
.Printf("[%q]", bytes
.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
450 // Output: ["Achtung! Achtung"]
453 func ExampleTrimFunc() {
454 fmt
.Println(string(bytes
.TrimFunc([]byte("go-gopher!"), unicode
.IsLetter
)))
455 fmt
.Println(string(bytes
.TrimFunc([]byte("\"go-gopher!\""), unicode
.IsLetter
)))
456 fmt
.Println(string(bytes
.TrimFunc([]byte("go-gopher!"), unicode
.IsPunct
)))
457 fmt
.Println(string(bytes
.TrimFunc([]byte("1234go-gopher!567"), unicode
.IsNumber
)))
465 func ExampleTrimLeft() {
466 fmt
.Print(string(bytes
.TrimLeft([]byte("453gopher8257"), "0123456789")))
471 func ExampleTrimLeftFunc() {
472 fmt
.Println(string(bytes
.TrimLeftFunc([]byte("go-gopher"), unicode
.IsLetter
)))
473 fmt
.Println(string(bytes
.TrimLeftFunc([]byte("go-gopher!"), unicode
.IsPunct
)))
474 fmt
.Println(string(bytes
.TrimLeftFunc([]byte("1234go-gopher!567"), unicode
.IsNumber
)))
481 func ExampleTrimPrefix() {
482 var b
= []byte("Goodbye,, world!")
483 b
= bytes
.TrimPrefix(b
, []byte("Goodbye,"))
484 b
= bytes
.TrimPrefix(b
, []byte("See ya,"))
485 fmt
.Printf("Hello%s", b
)
486 // Output: Hello, world!
489 func ExampleTrimSpace() {
490 fmt
.Printf("%s", bytes
.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
491 // Output: a lone gopher
494 func ExampleTrimSuffix() {
495 var b
= []byte("Hello, goodbye, etc!")
496 b
= bytes
.TrimSuffix(b
, []byte("goodbye, etc!"))
497 b
= bytes
.TrimSuffix(b
, []byte("gopher"))
498 b
= append(b
, bytes
.TrimSuffix([]byte("world!"), []byte("x!"))...)
500 // Output: Hello, world!
503 func ExampleTrimRight() {
504 fmt
.Print(string(bytes
.TrimRight([]byte("453gopher8257"), "0123456789")))
509 func ExampleTrimRightFunc() {
510 fmt
.Println(string(bytes
.TrimRightFunc([]byte("go-gopher"), unicode
.IsLetter
)))
511 fmt
.Println(string(bytes
.TrimRightFunc([]byte("go-gopher!"), unicode
.IsPunct
)))
512 fmt
.Println(string(bytes
.TrimRightFunc([]byte("1234go-gopher!567"), unicode
.IsNumber
)))
519 func ExampleToLower() {
520 fmt
.Printf("%s", bytes
.ToLower([]byte("Gopher")))
524 func ExampleToLowerSpecial() {
525 str
:= []byte("AHOJ VÝVOJÁRİ GOLANG")
526 totitle
:= bytes
.ToLowerSpecial(unicode
.AzeriCase
, str
)
527 fmt
.Println("Original : " + string(str
))
528 fmt
.Println("ToLower : " + string(totitle
))
530 // Original : AHOJ VÝVOJÁRİ GOLANG
531 // ToLower : ahoj vývojári golang
534 func ExampleToUpper() {
535 fmt
.Printf("%s", bytes
.ToUpper([]byte("Gopher")))
539 func ExampleToUpperSpecial() {
540 str
:= []byte("ahoj vývojári golang")
541 totitle
:= bytes
.ToUpperSpecial(unicode
.AzeriCase
, str
)
542 fmt
.Println("Original : " + string(str
))
543 fmt
.Println("ToUpper : " + string(totitle
))
545 // Original : ahoj vývojári golang
546 // ToUpper : AHOJ VÝVOJÁRİ GOLANG