aboutsummaryrefslogtreecommitdiff
path: root/src/bytes
diff options
context:
space:
mode:
authorcuishuang <imcusg@gmail.com>2025-02-13 15:28:21 +0800
committerGopher Robot <gobot@golang.org>2025-02-14 15:18:41 -0800
commit2299a4289d69c71573fd22350eea0677639e563c (patch)
tree2fa11ca7d587a36f16edfbf0b8cb045530b52637 /src/bytes
parentbad791343f50a165e27f9f9bda6ba42af05b1869 (diff)
downloadgo-2299a4289d69c71573fd22350eea0677639e563c.tar.xz
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 <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/bytes')
-rw-r--r--src/bytes/example_test.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/bytes/example_test.go b/src/bytes/example_test.go
index c9086d3918..71a4a9e2ca 100644
--- a/src/bytes/example_test.go
+++ b/src/bytes/example_test.go
@@ -628,3 +628,93 @@ func ExampleToUpperSpecial() {
// Original : ahoj vývojári golang
// ToUpper : AHOJ VÝVOJÁRİ GOLANG
}
+
+func ExampleLines() {
+ text := []byte("Hello\nWorld\nGo Programming\n")
+ for line := range bytes.Lines(text) {
+ fmt.Printf("%q\n", line)
+ }
+
+ // Output:
+ // "Hello\n"
+ // "World\n"
+ // "Go Programming\n"
+}
+
+func ExampleSplitSeq() {
+ s := []byte("a,b,c,d")
+ for part := range bytes.SplitSeq(s, []byte(",")) {
+ fmt.Printf("%q\n", part)
+ }
+
+ // Output:
+ // "a"
+ // "b"
+ // "c"
+ // "d"
+}
+
+func ExampleSplitAfterSeq() {
+ s := []byte("a,b,c,d")
+ for part := range bytes.SplitAfterSeq(s, []byte(",")) {
+ fmt.Printf("%q\n", part)
+ }
+
+ // Output:
+ // "a,"
+ // "b,"
+ // "c,"
+ // "d"
+}
+
+func ExampleFieldsSeq() {
+ text := []byte("The quick brown fox")
+ fmt.Println("Split byte slice into fields:")
+ for word := range bytes.FieldsSeq(text) {
+ fmt.Printf("%q\n", word)
+ }
+
+ textWithSpaces := []byte(" lots of spaces ")
+ fmt.Println("\nSplit byte slice with multiple spaces:")
+ for word := range bytes.FieldsSeq(textWithSpaces) {
+ fmt.Printf("%q\n", word)
+ }
+
+ // Output:
+ // Split byte slice into fields:
+ // "The"
+ // "quick"
+ // "brown"
+ // "fox"
+ //
+ // Split byte slice with multiple spaces:
+ // "lots"
+ // "of"
+ // "spaces"
+}
+
+func ExampleFieldsFuncSeq() {
+ text := []byte("The quick brown fox")
+ fmt.Println("Split on whitespace(similar to FieldsSeq):")
+ for word := range bytes.FieldsFuncSeq(text, unicode.IsSpace) {
+ fmt.Printf("%q\n", word)
+ }
+
+ mixedText := []byte("abc123def456ghi")
+ fmt.Println("\nSplit on digits:")
+ for word := range bytes.FieldsFuncSeq(mixedText, unicode.IsDigit) {
+ fmt.Printf("%q\n", word)
+ }
+
+ // Output:
+ // Split on whitespace(similar to FieldsSeq):
+ // "The"
+ // "quick"
+ // "brown"
+ // "fox"
+ //
+ // Split on digits:
+ // "abc"
+ // "def"
+ // "ghi"
+}