package main
import (
"bytes"
"fmt"
"reflect"
"strings"
)
func main() {
sentence := []byte("The Go language has built-in facilities, as well as library support, for writing concurrent programs.")
fmt.Printf("%q\n", sentence)
vowelsSpace := " "
chop := bytes.FieldsFunc(sentence, func(r rune) bool {
return strings.ContainsRune(vowelsSpace, r)
})
for index, element := range chop {
fmt.Printf("\n%d => %q", index, element)
}
fmt.Println("\n", "chop type : ", reflect.ValueOf(chop).Kind())
vowelsSpace = ","
chop = bytes.FieldsFunc(sentence, func(r rune) bool {
return strings.ContainsRune(vowelsSpace, r)
})
for index, element := range chop {
fmt.Printf("\n%d => %q", index, element)
}
fmt.Println("\n", "chop type : ", reflect.ValueOf(chop).Kind())
}
编译输出:
C:/Go/bin/go.exe build -i [D:/golang/src/JsonTest]
成功: 进程退出代码 0.
D:/golang/src/JsonTest/JsonTest.exe [D:/golang/src/JsonTest]
"The Go language has built-in facilities, as well as library support, for writing concurrent programs."
0 => "The"
1 => "Go"
2 => "language"
3 => "has"
4 => "built-in"
5 => "facilities,"
6 => "as"
7 => "well"
8 => "as"
9 => "library"
10 => "support,"
11 => "for"
12 => "writing"
13 => "concurrent"
14 => "programs."
chop type : slice
0 => "The Go language has built-in facilities"
1 => " as well as library support"
2 => " for writing concurrent programs."
chop type : slice
成功: 进程退出代码 0.