1package main 2 3import ( 4 "bytes" 5 "fmt" 6 "reflect" 7 "strings" 8) 9 10func main() { 11 12 sentence := []byte("The Go language has built-in facilities, as well as library support, for writing concurrent programs.") 13 fmt.Printf("%q\n", sentence) 14 15 vowelsSpace := " " 16 chop := bytes.FieldsFunc(sentence, func(r rune) bool { 17 return strings.ContainsRune(vowelsSpace, r) 18 }) 19 20 for index, element := range chop { 21 fmt.Printf("\n%d => %q", index, element) 22 } 23 fmt.Println("\n", "chop type : ", reflect.ValueOf(chop).Kind()) 24 25 vowelsSpace = "," 26 chop = bytes.FieldsFunc(sentence, func(r rune) bool { 27 return strings.ContainsRune(vowelsSpace, r) 28 }) 29 30 for index, element := range chop { 31 fmt.Printf("\n%d => %q", index, element) 32 } 33 fmt.Println("\n", "chop type : ", reflect.ValueOf(chop).Kind()) 34}
编译输出:
1C:/Go/bin/go.exe build -i [D:/golang/src/JsonTest] 2成功: 进程退出代码 0. 3D:/golang/src/JsonTest/JsonTest.exe [D:/golang/src/JsonTest] 4"The Go language has built-in facilities, as well as library support, for writing concurrent programs." 5 60 => "The" 71 => "Go" 82 => "language" 93 => "has" 104 => "built-in" 115 => "facilities," 126 => "as" 137 => "well" 148 => "as" 159 => "library" 1610 => "support," 1711 => "for" 1812 => "writing" 1913 => "concurrent" 2014 => "programs." 21 chop type : slice 22 230 => "The Go language has built-in facilities" 241 => " as well as library support" 252 => " for writing concurrent programs." 26 chop type : slice 27成功: 进程退出代码 0.