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 "unicode"
15 )
16
17 func ExampleBuffer() {
18 var b bytes.Buffer
19 b.Write([]byte("Hello "))
20 fmt.Fprintf(&b, "world!")
21 b.WriteTo(os.Stdout)
22
23 }
24
25 func ExampleBuffer_reader() {
26
27 buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==")
28 dec := base64.NewDecoder(base64.StdEncoding, buf)
29 io.Copy(os.Stdout, dec)
30
31 }
32
33 func ExampleBuffer_Bytes() {
34 buf := bytes.Buffer{}
35 buf.Write([]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'})
36 os.Stdout.Write(buf.Bytes())
37
38 }
39
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())
45
46
47
48 }
49
50 func ExampleBuffer_Grow() {
51 var b bytes.Buffer
52 b.Grow(64)
53 bb := b.Bytes()
54 b.Write([]byte("64 bytes or fewer"))
55 fmt.Printf("%q", bb[:b.Len()])
56
57 }
58
59 func ExampleBuffer_Len() {
60 var b bytes.Buffer
61 b.Grow(64)
62 b.Write([]byte("abcde"))
63 fmt.Printf("%d", b.Len())
64
65 }
66
67 func ExampleBuffer_Next() {
68 var b bytes.Buffer
69 b.Grow(64)
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)))
74
75
76
77
78 }
79
80 func ExampleBuffer_Read() {
81 var b bytes.Buffer
82 b.Grow(64)
83 b.Write([]byte("abcde"))
84 rdbuf := make([]byte, 1)
85 n, err := b.Read(rdbuf)
86 if err != nil {
87 panic(err)
88 }
89 fmt.Println(n)
90 fmt.Println(b.String())
91 fmt.Println(string(rdbuf))
92
93
94
95
96 }
97
98 func ExampleBuffer_ReadByte() {
99 var b bytes.Buffer
100 b.Grow(64)
101 b.Write([]byte("abcde"))
102 c, err := b.ReadByte()
103 if err != nil {
104 panic(err)
105 }
106 fmt.Println(c)
107 fmt.Println(b.String())
108
109
110
111 }
112
113 func ExampleCompare() {
114
115 var a, b []byte
116 if bytes.Compare(a, b) < 0 {
117
118 }
119 if bytes.Compare(a, b) <= 0 {
120
121 }
122 if bytes.Compare(a, b) > 0 {
123
124 }
125 if bytes.Compare(a, b) >= 0 {
126
127 }
128
129
130 if bytes.Equal(a, b) {
131
132 }
133 if !bytes.Equal(a, b) {
134
135 }
136 }
137
138 func ExampleCompare_search() {
139
140 var needle []byte
141 var haystack [][]byte
142 i := sort.Search(len(haystack), func(i int) bool {
143
144 return bytes.Compare(haystack[i], needle) >= 0
145 })
146 if i < len(haystack) && bytes.Equal(haystack[i], needle) {
147
148 }
149 }
150
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("")))
156
157
158
159
160
161 }
162
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(""), ""))
168
169
170
171
172
173 }
174
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(""), '@'))
181
182
183
184
185
186
187 }
188
189 func ExampleCount() {
190 fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
191 fmt.Println(bytes.Count([]byte("five"), []byte("")))
192
193
194
195 }
196
197 func ExampleCut() {
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)
201 }
202 show("Gopher", "Go")
203 show("Gopher", "ph")
204 show("Gopher", "er")
205 show("Gopher", "Badger")
206
207
208
209
210
211 }
212
213 func ExampleEqual() {
214 fmt.Println(bytes.Equal([]byte("Go"), []byte("Go")))
215 fmt.Println(bytes.Equal([]byte("Go"), []byte("C++")))
216
217
218
219 }
220
221 func ExampleEqualFold() {
222 fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
223
224 }
225
226 func ExampleFields() {
227 fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz ")))
228
229 }
230
231 func ExampleFieldsFunc() {
232 f := func(c rune) bool {
233 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
234 }
235 fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f))
236
237 }
238
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("")))
243
244
245
246
247 }
248
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("")))
254
255
256
257
258
259 }
260
261 func ExampleIndex() {
262 fmt.Println(bytes.Index([]byte("chicken"), []byte("ken")))
263 fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr")))
264
265
266
267 }
268
269 func ExampleIndexByte() {
270 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('k')))
271 fmt.Println(bytes.IndexByte([]byte("chicken"), byte('g')))
272
273
274
275 }
276
277 func ExampleIndexFunc() {
278 f := func(c rune) bool {
279 return unicode.Is(unicode.Han, c)
280 }
281 fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f))
282 fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f))
283
284
285
286 }
287
288 func ExampleIndexAny() {
289 fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy"))
290 fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy"))
291
292
293
294 }
295
296 func ExampleIndexRune() {
297 fmt.Println(bytes.IndexRune([]byte("chicken"), 'k'))
298 fmt.Println(bytes.IndexRune([]byte("chicken"), 'd'))
299
300
301
302 }
303
304 func ExampleJoin() {
305 s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
306 fmt.Printf("%s", bytes.Join(s, []byte(", ")))
307
308 }
309
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")))
314
315
316
317
318 }
319
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,!."))
324
325
326
327
328 }
329
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')))
334
335
336
337
338 }
339
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))
344
345
346
347
348 }
349
350 func ExampleReader_Len() {
351 fmt.Println(bytes.NewReader([]byte("Hi!")).Len())
352 fmt.Println(bytes.NewReader([]byte("こんにちは!")).Len())
353
354
355
356 }
357
358 func ExampleRepeat() {
359 fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2))
360
361 }
362
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))
366
367
368
369 }
370
371 func ExampleReplaceAll() {
372 fmt.Printf("%s\n", bytes.ReplaceAll([]byte("oink oink oink"), []byte("oink"), []byte("moo")))
373
374
375 }
376
377 func ExampleRunes() {
378 rs := bytes.Runes([]byte("go gopher"))
379 for _, r := range rs {
380 fmt.Printf("%#U\n", r)
381 }
382
383
384
385
386
387
388
389
390
391
392 }
393
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")))
399
400
401
402
403
404 }
405
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)
410
411
412
413 }
414
415 func ExampleSplitAfter() {
416 fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(",")))
417
418 }
419
420 func ExampleSplitAfterN() {
421 fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
422
423 }
424
425 func ExampleTitle() {
426 fmt.Printf("%s", bytes.Title([]byte("her royal highness")))
427
428 }
429
430 func ExampleToTitle() {
431 fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises")))
432 fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб")))
433
434
435
436 }
437
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))
443
444
445
446 }
447
448 func ExampleTrim() {
449 fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! "))
450
451 }
452
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)))
458
459
460
461
462
463 }
464
465 func ExampleTrimLeft() {
466 fmt.Print(string(bytes.TrimLeft([]byte("453gopher8257"), "0123456789")))
467
468
469 }
470
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)))
475
476
477
478
479 }
480
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
487 }
488
489 func ExampleTrimSpace() {
490 fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
491
492 }
493
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!"))...)
499 os.Stdout.Write(b)
500
501 }
502
503 func ExampleTrimRight() {
504 fmt.Print(string(bytes.TrimRight([]byte("453gopher8257"), "0123456789")))
505
506
507 }
508
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)))
513
514
515
516
517 }
518
519 func ExampleToLower() {
520 fmt.Printf("%s", bytes.ToLower([]byte("Gopher")))
521
522 }
523
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))
529
530
531
532 }
533
534 func ExampleToUpper() {
535 fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
536
537 }
538
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))
544
545
546
547 }
548
View as plain text