From f6b93a4c358b28b350dd8fe1780c1f78e520c09c Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Thu, 21 Mar 2024 13:50:37 +0800 Subject: [PATCH] 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 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Auto-Submit: Ian Lance Taylor Reviewed-by: Ian Lance Taylor --- src/slices/example_test.go | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/slices/example_test.go b/src/slices/example_test.go index 41d8890b57c634..76ebe0dfaccbab 100644 --- a/src/slices/example_test.go +++ b/src/slices/example_test.go @@ -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] +}