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 "slices"
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 _, found := slices.BinarySearchFunc(haystack, needle, bytes.Compare)
169 if found {
170
171 }
172 }
173
174 func ExampleContains() {
175 fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo")))
176 fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar")))
177 fmt.Println(bytes.Contains([]byte("seafood"), []byte("")))
178 fmt.Println(bytes.Contains([]byte(""), []byte("")))
179
180
181
182
183
184 }
185
186 func ExampleContainsAny() {
187 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!"))
188 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的."))
189 fmt.Println(bytes.ContainsAny([]byte("I like seafood."), ""))
190 fmt.Println(bytes.ContainsAny([]byte(""), ""))
191
192
193
194
195
196 }
197
198 func ExampleContainsRune() {
199 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
200 fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
201 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大'))
202 fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!'))
203 fmt.Println(bytes.ContainsRune([]byte(""), '@'))
204
205
206
207
208
209
210 }
211
212 func ExampleContainsFunc() {
213 f := func(r rune) bool {
214 return r >= 'a' && r <= 'z'
215 }
216 fmt.Println(bytes.ContainsFunc([]byte("HELLO"), f))
217 fmt.Println(bytes.ContainsFunc([]byte("World"), f))
218
219
220
221 }
222
223 func ExampleCount() {
224 fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
225 fmt.Println(bytes.Count([]byte("five"), []byte("")))
226
227
228
229 }
230
231 func ExampleCut() {
232 show := func(s, sep string) {
233 before, after, found := bytes.Cut([]byte(s), []byte(sep))
234 fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
235 }
236 show("Gopher", "Go")
237 show("Gopher", "ph")
238 show("Gopher", "er")
239 show("Gopher", "Badger")
240
241
242
243
244
245 }
246
247 func ExampleCutPrefix() {
248 show := func(s, sep string) {
249 after, found := bytes.CutPrefix([]byte(s), []byte(sep))
250 fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found)
251 }
252 show("Gopher", "Go")
253 show("Gopher", "ph")
254
255
256
257 }
258
259 func ExampleCutSuffix() {
260 show := func(s, sep string) {
261 before, found := bytes.CutSuffix([]byte(s), []byte(sep))
262 fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, before, found)
263 }
264 show("Gopher", "Go")
265 show("Gopher", "er")
266
267
268
269 }
270
271 func ExampleEqual() {
272 fmt.Println(bytes.Equal([]byte("Go"), []byte("Go")))
273 fmt.Println(bytes.Equal([]byte("Go"), []byte("C++")))
274
275
276
277 }
278
279 func ExampleEqualFold() {
280 fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
281
282 }
283
284 func ExampleFields() {
285 fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz ")))
286
287 }
288
289 func ExampleFieldsFunc() {
290 f := func(c rune) bool {
291 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
292 }
293 fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f))
294
295 }
296
297 func ExampleHasPrefix() {
298 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go")))
299 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C")))
300 fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("")))
301
302
303
304
305 }
306
307 func ExampleHasSuffix() {
308 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go")))
309 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O")))
310 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami")))
311 fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("")))
312
313
314
315
316
317 }
318
319 func ExampleIndex() {
320 fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
321 fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
322
323
324
325 }
326
327 func ExampleIndexByte() {
328 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k')))
329 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g')))
330
331
332
333 }
334
335 func ExampleIndexFunc() {
336 f := func(c rune) bool {
337 return unicode.Is(unicode.Han, c)
338 }
339 fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f))
340 fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f))
341
342
343
344 }
345
346 func ExampleIndexAny() {
347 fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
348 fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
349
350
351
352 }
353
354 func ExampleIndexRune() {
355 fmt.Println(bytes.IndexRune([]byte("chicken"), 'k'))
356 fmt.Println(bytes.IndexRune([]byte("chicken"), 'd'))
357
358
359
360 }
361
362 func ExampleJoin() {
363 s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
364 fmt.Printf("%s", bytes.Join(s, []byte(", ")))
365
366 }
367
368 func ExampleLastIndex() {
369 fmt.Println(bytes.Index([]byte("go gopher"), []byte("go")))
370 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go")))
371 fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent")))
372
373
374
375
376 }
377
378 func ExampleLastIndexAny() {
379 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp"))
380 fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大"))
381 fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!."))
382
383
384
385
386 }
387
388 func ExampleLastIndexByte() {
389 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g')))
390 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r')))
391 fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z')))
392
393
394
395
396 }
397
398 func ExampleLastIndexFunc() {
399 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter))
400 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct))
401 fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber))
402
403
404
405
406 }
407
408 func ExampleMap() {
409 rot13 := func(r rune) rune {
410 switch {
411 case r >= 'A' && r <= 'Z':
412 return 'A' + (r-'A'+13)%26
413 case r >= 'a' && r <= 'z':
414 return 'a' + (r-'a'+13)%26
415 }
416 return r
417 }
418 fmt.Printf("%s\n", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher...")))
419
420
421 }
422
423 func ExampleReader_Len() {
424 fmt.Println(bytes.NewReader([]byte("Hi!")).Len())
425 fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len())
426
427
428
429 }
430
431 func ExampleRepeat() {
432 fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2))
433
434 }
435
436 func ExampleReplace() {
437 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
438 fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
439
440
441
442 }
443
444 func ExampleReplaceAll() {
445 fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo")))
446
447
448 }
449
450 func ExampleRunes() {
451 rs := bytes.Runes([]byte("go gopher"))
452 for _, r := range rs {
453 fmt.Printf("%#U\n", r)
454 }
455
456
457
458
459
460
461
462
463
464
465 }
466
467 func ExampleSplit() {
468 fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
469 fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
470 fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte("")))
471 fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins")))
472
473
474
475
476
477 }
478
479 func ExampleSplitN() {
480 fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2))
481 z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0)
482 fmt.Printf("%q (nil = %v)\n", z, z == nil)
483
484
485
486 }
487
488 func ExampleSplitAfter() {
489 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(",")))
490
491 }
492
493 func ExampleSplitAfterN() {
494 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
495
496 }
497
498 func ExampleTitle() {
499 fmt.Printf("%s", bytes.Title([]byte("her royal highness")))
500
501 }
502
503 func ExampleToTitle() {
504 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises")))
505 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб")))
506
507
508
509 }
510
511 func ExampleToTitleSpecial() {
512 str := []byte("ahoj vývojári golang")
513 totitle := bytes.ToTitleSpecial(unicode.AzeriCase, str)
514 fmt.Println("Original : " + string(str))
515 fmt.Println("ToTitle : " + string(totitle))
516
517
518
519 }
520
521 func ExampleToValidUTF8() {
522 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("abc"), []byte("\uFFFD")))
523 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("a\xffb\xC0\xAFc\xff"), []byte("")))
524 fmt.Printf("%s\n", bytes.ToValidUTF8([]byte("\xed\xa0\x80"), []byte("abc")))
525
526
527
528
529 }
530
531 func ExampleTrim() {
532 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
533
534 }
535
536 func ExampleTrimFunc() {
537 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter)))
538 fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter)))
539 fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct)))
540 fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
541
542
543
544
545
546 }
547
548 func ExampleTrimLeft() {
549 fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789")))
550
551
552 }
553
554 func ExampleTrimLeftFunc() {
555 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter)))
556 fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct)))
557 fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
558
559
560
561
562 }
563
564 func ExampleTrimPrefix() {
565 var b = []byte("Goodbye,, world!")
566 b = bytes.TrimPrefix(b, []byte("Goodbye,"))
567 b = bytes.TrimPrefix(b, []byte("See ya,"))
568 fmt.Printf("Hello%s", b)
569
570 }
571
572 func ExampleTrimSpace() {
573 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
574
575 }
576
577 func ExampleTrimSuffix() {
578 var b = []byte("Hello, goodbye, etc!")
579 b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
580 b = bytes.TrimSuffix(b, []byte("gopher"))
581 b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
582 os.Stdout.Write(b)
583
584 }
585
586 func ExampleTrimRight() {
587 fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789")))
588
589
590 }
591
592 func ExampleTrimRightFunc() {
593 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter)))
594 fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct)))
595 fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
596
597
598
599
600 }
601
602 func ExampleToLower() {
603 fmt.Printf("%s", bytes.ToLower([]byte("Gopher")))
604
605 }
606
607 func ExampleToLowerSpecial() {
608 str := []byte("AHOJ VÝVOJÁRİ GOLANG")
609 totitle := bytes.ToLowerSpecial(unicode.AzeriCase, str)
610 fmt.Println("Original : " + string(str))
611 fmt.Println("ToLower : " + string(totitle))
612
613
614
615 }
616
617 func ExampleToUpper() {
618 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
619
620 }
621
622 func ExampleToUpperSpecial() {
623 str := []byte("ahoj vývojári golang")
624 totitle := bytes.ToUpperSpecial(unicode.AzeriCase, str)
625 fmt.Println("Original : " + string(str))
626 fmt.Println("ToUpper : " + string(totitle))
627
628
629
630 }
631
View as plain text