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