Source file
src/bytes/example_test.go
Documentation: bytes
1
2
3
4
5 package bytes_test
6
7 import (
8 "bytes"
9 "encoding/base64"
10 "fmt"
11 "io"
12 "os"
13 "sort"
14 "strconv"
15 "unicode"
16 )
17
18 func ExampleBuffer() {
19 var b bytes.Buffer
20 b.Write([]byte("Hello "))
21 fmt.Fprintf(&b, "world!")
22 b.WriteTo(os.Stdout)
23
24 }
25
26 func ExampleBuffer_reader() {
27
28 buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==")
29 dec := base64.NewDecoder(base64.StdEncoding, buf)
30 io.Copy(os.Stdout, dec)
31
32 }
33
34 func ExampleBuffer_Bytes() {
35 buf := bytes.Buffer{}
36 buf.Write([]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'})
37 os.Stdout.Write(buf.Bytes())
38
39 }
40
41 func ExampleBuffer_AvailableBuffer() {
42 var buf bytes.Buffer
43 for i := 0; i < 4; i++ {
44 b := buf.AvailableBuffer()
45 b = strconv.AppendInt(b, int64(i), 10)
46 b = append(b, ' ')
47 buf.Write(b)
48 }
49 os.Stdout.Write(buf.Bytes())
50
51 }
52
53 func ExampleBuffer_Cap() {
54 buf1 := bytes.NewBuffer(make([]byte, 10))
55 buf2 := bytes.NewBuffer(make([]byte, 0, 10))
56 fmt.Println(buf1.Cap())
57 fmt.Println(buf2.Cap())
58
59
60
61 }
62
63 func ExampleBuffer_Grow() {
64 var b bytes.Buffer
65 b.Grow(64)
66 bb := b.Bytes()
67 b.Write([]byte("64 bytes or fewer"))
68 fmt.Printf("%q", bb[:b.Len()])
69
70 }
71
72 func ExampleBuffer_Len() {
73 var b bytes.Buffer
74 b.Grow(64)
75 b.Write([]byte("abcde"))
76 fmt.Printf("%d", b.Len())
77
78 }
79
80 func ExampleBuffer_Next() {
81 var b bytes.Buffer
82 b.Grow(64)
83 b.Write([]byte("abcde"))
84 fmt.Printf("%s\n", b.Next(2))
85 fmt.Printf("%s\n", b.Next(2))
86 fmt.Printf("%s", b.Next(2))
87
88
89
90
91 }
92
93 func ExampleBuffer_Read() {
94 var b bytes.Buffer
95 b.Grow(64)
96 b.Write([]byte("abcde"))
97 rdbuf := make([]byte, 1)
98 n, err := b.Read(rdbuf)
99 if err != nil {
100 panic(err)
101 }
102 fmt.Println(n)
103 fmt.Println(b.String())
104 fmt.Println(string(rdbuf))
105
106
107
108
109 }
110
111 func ExampleBuffer_ReadByte() {
112 var b bytes.Buffer
113 b.Grow(64)
114 b.Write([]byte("abcde"))
115 c, err := b.ReadByte()
116 if err != nil {
117 panic(err)
118 }
119 fmt.Println(c)
120 fmt.Println(b.String())
121
122
123
124 }
125
126 func ExampleClone() {
127 b := []byte("abc")
128 clone := bytes.Clone(b)
129 fmt.Printf("%s\n", clone)
130 clone[0] = 'd'
131 fmt.Printf("%s\n", b)
132 fmt.Printf("%s\n", clone)
133
134
135
136
137 }
138
139 func ExampleCompare() {
140
141 var a, b []byte
142 if bytes.Compare(a, b) < 0 {
143
144 }
145 if bytes.Compare(a, b) <= 0 {
146
147 }
148 if bytes.Compare(a, b) > 0 {
149
150 }
151 if bytes.Compare(a, b) >= 0 {
152
153 }
154
155
156 if bytes.Equal(a, b) {
157
158 }
159 if !bytes.Equal(a, b) {
160
161 }
162 }
163
164 func ExampleCompare_search() {
165
166 var needle []byte
167 var haystack [][]byte
168 i := sort.Search(len(haystack), func(i int) bool {
169
170 return bytes.Compare(haystack[i], needle) >= 0
171 })
172 if i < len(haystack) && bytes.Equal(haystack[i], needle) {
173
174 }
175 }
176
177 func ExampleContains() {
178 fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo")))
179 fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar")))
180 fmt.Println(bytes.Contains([]byte("seafood"), []byte("")))
181 fmt.Println(bytes.Contains([]byte(""), []byte("")))
182
183
184
185
186
187 }
188
189 func ExampleContainsAny() {
190 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!"))
191 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的."))
192 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), ""))
193 fmt.Println(bytes.ContainsAny([]byte(""), ""))
194
195
196
197
198
199 }
200
201 func ExampleContainsRune() {
202 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
203 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
204 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大'))
205 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!'))
206 fmt.Println(bytes.ContainsRune([]byte(""), '@'))
207
208
209
210
211
212
213 }
214
215 func ExampleContainsFunc() {
216 f := func(r rune) bool {
217 return r >= 'a' && r <= 'z'
218 }
219 fmt.Println(bytes.ContainsFunc([]byte("HELLO"), f))
220 fmt.Println(bytes.ContainsFunc([]byte("World"), f))
221
222
223
224 }
225
226 func ExampleCount() {
227 fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
228 fmt.Println(bytes.Count([]byte("five"), []byte("")))
229
230
231
232 }
233
234 func ExampleCut() {
235 show := func(s, sep string) {
236 before, after, found := bytes.Cut([]byte(s), []byte(sep))
237 fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
238 }
239 show("Gopher", "Go")
240 show("Gopher", "ph")
241 show("Gopher", "er")
242 show("Gopher", "Badger")
243
244
245
246
247
248 }
249
250 func ExampleCutPrefix() {
251 show := func(s, sep string) {
252 after, found := bytes.CutPrefix([]byte(s), []byte(sep))
253 fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found)
254 }
255 show("Gopher", "Go")
256 show("Gopher", "ph")
257
258
259
260 }
261
262 func ExampleCutSuffix() {
263 show := func(s, sep string) {
264 before, found := bytes.CutSuffix([]byte(s), []byte(sep))
265 fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, before, found)
266 }
267 show("Gopher", "Go")
268 show("Gopher", "er")
269
270
271
272 }
273
274 func ExampleEqual() {
275 fmt.Println(bytes.Equal([]byte("Go"), []byte("Go")))
276 fmt.Println(bytes.Equal([]byte("Go"), []byte("C++")))
277
278
279
280 }
281
282 func ExampleEqualFold() {
283 fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
284
285 }
286
287 func ExampleFields() {
288 fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz ")))
289
290 }
291
292 func ExampleFieldsFunc() {
293 f := func(c rune) bool {
294 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
295 }
296 fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f))
297
298 }
299
300 func ExampleHasPrefix() {
301 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go")))
302 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C")))
303 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("")))
304
305
306
307
308 }
309
310 func ExampleHasSuffix() {
311 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go")))
312 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O")))
313 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami")))
314 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("")))
315
316
317
318
319
320 }
321
322 func ExampleIndex() {
323 fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
324 fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
325
326
327
328 }
329
330 func ExampleIndexByte() {
331 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k')))
332 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g')))
333
334
335
336 }
337
338 func ExampleIndexFunc() {
339 f := func(c rune) bool {
340 return unicode.Is(unicode.Han, c)
341 }
342 fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f))
343 fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f))
344
345
346
347 }
348
349 func ExampleIndexAny() {
350 fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
351 fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
352
353
354
355 }
356
357 func ExampleIndexRune() {
358 fmt.Println(bytes.IndexRune([]byte("chicken"), 'k'))
359 fmt.Println(bytes.IndexRune([]byte("chicken"), 'd'))
360
361
362
363 }
364
365 func ExampleJoin() {
366 s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
367 fmt.Printf("%s", bytes.Join(s, []byte(", ")))
368
369 }
370
371 func ExampleLastIndex() {
372 fmt.Println(bytes.Index([]byte("go gopher"), []byte("go")))
373 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go")))
374 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent")))
375
376
377
378
379 }
380
381 func ExampleLastIndexAny() {
382 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp"))
383 fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大"))
384 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!."))
385
386
387
388
389 }
390
391 func ExampleLastIndexByte() {
392 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g')))
393 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r')))
394 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z')))
395
396
397
398
399 }
400
401 func ExampleLastIndexFunc() {
402 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter))
403 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct))
404 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber))
405
406
407
408
409 }
410
411 func ExampleMap() {
412 rot13 := func(r rune) rune {
413 switch {
414 case r >= 'A' && r <= 'Z':
415 return 'A' + (r-'A'+13)%26
416 case r >= 'a' && r <= 'z':
417 return 'a' + (r-'a'+13)%26
418 }
419 return r
420 }
421 fmt.Printf("%s\n", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher...")))
422
423
424 }
425
426 func ExampleReader_Len() {
427 fmt.Println(bytes.NewReader([]byte("Hi!")).Len())
428 fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len())
429
430
431
432 }
433
434 func ExampleRepeat() {
435 fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2))
436
437 }
438
439 func ExampleReplace() {
440 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
441 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
442
443
444
445 }
446
447 func ExampleReplaceAll() {
448 fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo")))
449
450
451 }
452
453 func ExampleRunes() {
454 rs := bytes.Runes([]byte("go gopher"))
455 for _, r := range rs {
456 fmt.Printf("%#U\n", r)
457 }
458
459
460
461
462
463
464
465
466
467
468 }
469
470 func ExampleSplit() {
471 fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
472 fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
473 fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte("")))
474 fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins")))
475
476
477
478
479
480 }
481
482 func ExampleSplitN() {
483 fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2))
484 z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0)
485 fmt.Printf("%q (nil = %v)\n", z, z == nil)
486
487
488
489 }
490
491 func ExampleSplitAfter() {
492 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(",")))
493
494 }
495
496 func ExampleSplitAfterN() {
497 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
498
499 }
500
501 func ExampleTitle() {
502 fmt.Printf("%s", bytes.Title([]byte("her royal highness")))
503
504 }
505
506 func ExampleToTitle() {
507 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises")))
508 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб")))
509
510
511
512 }
513
514 func ExampleToTitleSpecial() {
515 str := []byte("ahoj vývojári golang")
516 totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str)
517 fmt.Println("Original : " + string(str))
518 fmt.Println("ToTitle : " + string(totitle))
519
520
521
522 }
523
524 func ExampleToValidUTF8() {
525 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("abc"), []byte("\uFFFD")))
526 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("a\xffb\xC0\xAFc\xff"), []byte("")))
527 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("\xed\xa0\x80"), []byte("abc")))
528
529
530
531
532 }
533
534 func ExampleTrim() {
535 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
536
537 }
538
539 func ExampleTrimFunc() {
540 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter)))
541 fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter)))
542 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct)))
543 fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
544
545
546
547
548
549 }
550
551 func ExampleTrimLeft() {
552 fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789")))
553
554
555 }
556
557 func ExampleTrimLeftFunc() {
558 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter)))
559 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct)))
560 fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
561
562
563
564
565 }
566
567 func ExampleTrimPrefix() {
568 var b = []byte("Goodbye,, world!")
569 b = bytes.TrimPrefix(b, []byte("Goodbye,"))
570 b = bytes.TrimPrefix(b, []byte("See ya,"))
571 fmt.Printf("Hello%s", b)
572
573 }
574
575 func ExampleTrimSpace() {
576 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
577
578 }
579
580 func ExampleTrimSuffix() {
581 var b = []byte("Hello, goodbye, etc!")
582 b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
583 b = bytes.TrimSuffix(b, []byte("gopher"))
584 b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
585 os.Stdout.Write(b)
586
587 }
588
589 func ExampleTrimRight() {
590 fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789")))
591
592
593 }
594
595 func ExampleTrimRightFunc() {
596 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter)))
597 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct)))
598 fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
599
600
601
602
603 }
604
605 func ExampleToLower() {
606 fmt.Printf("%s", bytes.ToLower([]byte("Gopher")))
607
608 }
609
610 func ExampleToLowerSpecial() {
611 str := []byte("AHOJ VÝVOJÁRİ GOLANG")
612 totitle := bytes.ToLowerSpecial(unicode.AzeriCase, str)
613 fmt.Println("Original : " + string(str))
614 fmt.Println("ToLower : " + string(totitle))
615
616
617
618 }
619
620 func ExampleToUpper() {
621 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
622
623 }
624
625 func ExampleToUpperSpecial() {
626 str := []byte("ahoj vývojári golang")
627 totitle := bytes.ToUpperSpecial(unicode.AzeriCase, str)
628 fmt.Println("Original : " + string(str))
629 fmt.Println("ToUpper : " + string(totitle))
630
631
632
633 }
634
View as plain text