mirror of
https://github.com/golang/go.git
synced 2025-05-28 10:51:22 +00:00
slices: add examples
For Clone, Grow, Clip, Concat, Contains, Repeat. Fixes #66435 Change-Id: Ife8f61427e9cd18b7106c100de8f82f9d7840c9b Reviewed-on: https://go-review.googlesource.com/c/go/+/573255 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
aab837dd46
commit
f6b93a4c35
@ -318,3 +318,69 @@ func ExampleSortStableFunc() {
|
||||
// Output:
|
||||
// [{Alice 20} {Alice 55} {Bob 24} {Gopher 13}]
|
||||
}
|
||||
|
||||
func ExampleClone() {
|
||||
numbers := []int{0, 42, -10, 8}
|
||||
clone := slices.Clone(numbers)
|
||||
fmt.Println(clone)
|
||||
clone[2] = 10
|
||||
fmt.Println(numbers)
|
||||
// Output:
|
||||
// [0 42 -10 8]
|
||||
// [0 42 -10 8]
|
||||
}
|
||||
|
||||
func ExampleGrow() {
|
||||
numbers := []int{0, 42, -10, 8}
|
||||
grow := slices.Grow(numbers, 2)
|
||||
fmt.Println(cap(numbers))
|
||||
fmt.Println(grow)
|
||||
fmt.Println(len(grow))
|
||||
fmt.Println(cap(grow))
|
||||
// Output:
|
||||
// 4
|
||||
// [0 42 -10 8]
|
||||
// 4
|
||||
// 8
|
||||
}
|
||||
|
||||
func ExampleClip() {
|
||||
a := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
s := a[:4:10]
|
||||
clip := slices.Clip(s)
|
||||
fmt.Println(cap(s))
|
||||
fmt.Println(clip)
|
||||
fmt.Println(len(clip))
|
||||
fmt.Println(cap(clip))
|
||||
// Output:
|
||||
// 10
|
||||
// [0 1 2 3]
|
||||
// 4
|
||||
// 4
|
||||
}
|
||||
|
||||
func ExampleConcat() {
|
||||
s1 := []int{0, 1, 2, 3}
|
||||
s2 := []int{4, 5, 6}
|
||||
concat := slices.Concat(s1, s2)
|
||||
fmt.Println(concat)
|
||||
// Output:
|
||||
// [0 1 2 3 4 5 6]
|
||||
}
|
||||
|
||||
func ExampleContains() {
|
||||
numbers := []int{0, 1, 2, 3}
|
||||
fmt.Println(slices.Contains(numbers, 2))
|
||||
fmt.Println(slices.Contains(numbers, 4))
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleRepeat() {
|
||||
numbers := []int{0, 1, 2, 3}
|
||||
repeat := slices.Repeat(numbers, 2)
|
||||
fmt.Println(repeat)
|
||||
// Output:
|
||||
// [0 1 2 3 0 1 2 3]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user