Skip to content

Commit 2299a42

Browse files
cuishuanggopherbot
authored andcommitted
bytes: add examples for Lines, SplitSeq, SplitAfterSeq, FieldsSeq and FieldsFuncSeq
Change-Id: I0e755d5c73f14d2c98853bdd31a7f2e84c92a906 Reviewed-on: https://go-review.googlesource.com/c/go/+/648860 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent bad7913 commit 2299a42

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

src/bytes/example_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,93 @@ func ExampleToUpperSpecial() {
628628
// Original : ahoj vývojári golang
629629
// ToUpper : AHOJ VÝVOJÁRİ GOLANG
630630
}
631+
632+
func ExampleLines() {
633+
text := []byte("Hello\nWorld\nGo Programming\n")
634+
for line := range bytes.Lines(text) {
635+
fmt.Printf("%q\n", line)
636+
}
637+
638+
// Output:
639+
// "Hello\n"
640+
// "World\n"
641+
// "Go Programming\n"
642+
}
643+
644+
func ExampleSplitSeq() {
645+
s := []byte("a,b,c,d")
646+
for part := range bytes.SplitSeq(s, []byte(",")) {
647+
fmt.Printf("%q\n", part)
648+
}
649+
650+
// Output:
651+
// "a"
652+
// "b"
653+
// "c"
654+
// "d"
655+
}
656+
657+
func ExampleSplitAfterSeq() {
658+
s := []byte("a,b,c,d")
659+
for part := range bytes.SplitAfterSeq(s, []byte(",")) {
660+
fmt.Printf("%q\n", part)
661+
}
662+
663+
// Output:
664+
// "a,"
665+
// "b,"
666+
// "c,"
667+
// "d"
668+
}
669+
670+
func ExampleFieldsSeq() {
671+
text := []byte("The quick brown fox")
672+
fmt.Println("Split byte slice into fields:")
673+
for word := range bytes.FieldsSeq(text) {
674+
fmt.Printf("%q\n", word)
675+
}
676+
677+
textWithSpaces := []byte(" lots of spaces ")
678+
fmt.Println("\nSplit byte slice with multiple spaces:")
679+
for word := range bytes.FieldsSeq(textWithSpaces) {
680+
fmt.Printf("%q\n", word)
681+
}
682+
683+
// Output:
684+
// Split byte slice into fields:
685+
// "The"
686+
// "quick"
687+
// "brown"
688+
// "fox"
689+
//
690+
// Split byte slice with multiple spaces:
691+
// "lots"
692+
// "of"
693+
// "spaces"
694+
}
695+
696+
func ExampleFieldsFuncSeq() {
697+
text := []byte("The quick brown fox")
698+
fmt.Println("Split on whitespace(similar to FieldsSeq):")
699+
for word := range bytes.FieldsFuncSeq(text, unicode.IsSpace) {
700+
fmt.Printf("%q\n", word)
701+
}
702+
703+
mixedText := []byte("abc123def456ghi")
704+
fmt.Println("\nSplit on digits:")
705+
for word := range bytes.FieldsFuncSeq(mixedText, unicode.IsDigit) {
706+
fmt.Printf("%q\n", word)
707+
}
708+
709+
// Output:
710+
// Split on whitespace(similar to FieldsSeq):
711+
// "The"
712+
// "quick"
713+
// "brown"
714+
// "fox"
715+
//
716+
// Split on digits:
717+
// "abc"
718+
// "def"
719+
// "ghi"
720+
}

0 commit comments

Comments
 (0)