mirror of
https://github.com/golang/go
synced 2024-11-19 15:54:46 -07:00
bytes: Add missing examples to functions
Fixes #21570 Change-Id: Ia0734929a04fbce8fdd5fbcb1b7baff9a8bbe39e Reviewed-on: https://go-review.googlesource.com/58030 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
2bba267120
commit
394f6a5ac0
@ -119,6 +119,32 @@ func ExampleContains() {
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleContainsAny() {
|
||||
fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "fÄo!"))
|
||||
fmt.Println(bytes.ContainsAny([]byte("I like seafood."), "去是伟大的."))
|
||||
fmt.Println(bytes.ContainsAny([]byte("I like seafood."), ""))
|
||||
fmt.Println(bytes.ContainsAny([]byte(""), ""))
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleContainsRune() {
|
||||
fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
|
||||
fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
|
||||
fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '大'))
|
||||
fmt.Println(bytes.ContainsRune([]byte("去是伟大的!"), '!'))
|
||||
fmt.Println(bytes.ContainsRune([]byte(""), '@'))
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleCount() {
|
||||
fmt.Println(bytes.Count([]byte("cheese"), []byte("e")))
|
||||
fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune
|
||||
@ -199,6 +225,36 @@ func ExampleLastIndex() {
|
||||
// -1
|
||||
}
|
||||
|
||||
func ExampleLastIndexAny() {
|
||||
fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "MüQp"))
|
||||
fmt.Println(bytes.LastIndexAny([]byte("go 地鼠"), "地大"))
|
||||
fmt.Println(bytes.LastIndexAny([]byte("go gopher"), "z,!."))
|
||||
// Output:
|
||||
// 5
|
||||
// 3
|
||||
// -1
|
||||
}
|
||||
|
||||
func ExampleLastIndexByte() {
|
||||
fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('g')))
|
||||
fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('r')))
|
||||
fmt.Println(bytes.LastIndexByte([]byte("go gopher"), byte('z')))
|
||||
// Output:
|
||||
// 3
|
||||
// 8
|
||||
// -1
|
||||
}
|
||||
|
||||
func ExampleLastIndexFunc() {
|
||||
fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsLetter))
|
||||
fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsPunct))
|
||||
fmt.Println(bytes.LastIndexFunc([]byte("go gopher!"), unicode.IsNumber))
|
||||
// Output:
|
||||
// 8
|
||||
// 9
|
||||
// -1
|
||||
}
|
||||
|
||||
func ExampleJoin() {
|
||||
s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
|
||||
fmt.Printf("%s", bytes.Join(s, []byte(", ")))
|
||||
@ -218,6 +274,23 @@ func ExampleReplace() {
|
||||
// moo moo moo
|
||||
}
|
||||
|
||||
func ExampleRunes() {
|
||||
rs := bytes.Runes([]byte("go gopher"))
|
||||
for _, r := range rs {
|
||||
fmt.Printf("%#U\n", r)
|
||||
}
|
||||
// Output:
|
||||
// U+0067 'g'
|
||||
// U+006F 'o'
|
||||
// U+0020 ' '
|
||||
// U+0067 'g'
|
||||
// U+006F 'o'
|
||||
// U+0070 'p'
|
||||
// U+0068 'h'
|
||||
// U+0065 'e'
|
||||
// U+0072 'r'
|
||||
}
|
||||
|
||||
func ExampleSplit() {
|
||||
fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(",")))
|
||||
fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a ")))
|
||||
@ -267,6 +340,18 @@ func ExampleTrim() {
|
||||
// Output: ["Achtung! Achtung"]
|
||||
}
|
||||
|
||||
func ExampleTrimFunc() {
|
||||
fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsLetter)))
|
||||
fmt.Println(string(bytes.TrimFunc([]byte("\"go-gopher!\""), unicode.IsLetter)))
|
||||
fmt.Println(string(bytes.TrimFunc([]byte("go-gopher!"), unicode.IsPunct)))
|
||||
fmt.Println(string(bytes.TrimFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
|
||||
// Output:
|
||||
// -gopher!
|
||||
// "go-gopher!"
|
||||
// go-gopher
|
||||
// go-gopher!
|
||||
}
|
||||
|
||||
func ExampleMap() {
|
||||
rot13 := func(r rune) rune {
|
||||
switch {
|
||||
@ -287,6 +372,16 @@ func ExampleTrimLeft() {
|
||||
// 5400
|
||||
}
|
||||
|
||||
func ExampleTrimLeftFunc() {
|
||||
fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher"), unicode.IsLetter)))
|
||||
fmt.Println(string(bytes.TrimLeftFunc([]byte("go-gopher!"), unicode.IsPunct)))
|
||||
fmt.Println(string(bytes.TrimLeftFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
|
||||
// Output:
|
||||
// -gopher
|
||||
// go-gopher!
|
||||
// go-gopher!567
|
||||
}
|
||||
|
||||
func ExampleTrimSpace() {
|
||||
fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n")))
|
||||
// Output: a lone gopher
|
||||
@ -298,6 +393,16 @@ func ExampleTrimRight() {
|
||||
// 453gopher
|
||||
}
|
||||
|
||||
func ExampleTrimRightFunc() {
|
||||
fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher"), unicode.IsLetter)))
|
||||
fmt.Println(string(bytes.TrimRightFunc([]byte("go-gopher!"), unicode.IsPunct)))
|
||||
fmt.Println(string(bytes.TrimRightFunc([]byte("1234go-gopher!567"), unicode.IsNumber)))
|
||||
// Output:
|
||||
// go-
|
||||
// go-gopher
|
||||
// 1234go-gopher!
|
||||
}
|
||||
|
||||
func ExampleToUpper() {
|
||||
fmt.Printf("%s", bytes.ToUpper([]byte("Gopher")))
|
||||
// Output: GOPHER
|
||||
|
Loading…
Reference in New Issue
Block a user