Skip to content

Commit f6b93a4

Browse files
callthingsoffgopherbot
authored andcommitted
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent aab837d commit f6b93a4

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/slices/example_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,69 @@ func ExampleSortStableFunc() {
318318
// Output:
319319
// [{Alice 20} {Alice 55} {Bob 24} {Gopher 13}]
320320
}
321+
322+
func ExampleClone() {
323+
numbers := []int{0, 42, -10, 8}
324+
clone := slices.Clone(numbers)
325+
fmt.Println(clone)
326+
clone[2] = 10
327+
fmt.Println(numbers)
328+
// Output:
329+
// [0 42 -10 8]
330+
// [0 42 -10 8]
331+
}
332+
333+
func ExampleGrow() {
334+
numbers := []int{0, 42, -10, 8}
335+
grow := slices.Grow(numbers, 2)
336+
fmt.Println(cap(numbers))
337+
fmt.Println(grow)
338+
fmt.Println(len(grow))
339+
fmt.Println(cap(grow))
340+
// Output:
341+
// 4
342+
// [0 42 -10 8]
343+
// 4
344+
// 8
345+
}
346+
347+
func ExampleClip() {
348+
a := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
349+
s := a[:4:10]
350+
clip := slices.Clip(s)
351+
fmt.Println(cap(s))
352+
fmt.Println(clip)
353+
fmt.Println(len(clip))
354+
fmt.Println(cap(clip))
355+
// Output:
356+
// 10
357+
// [0 1 2 3]
358+
// 4
359+
// 4
360+
}
361+
362+
func ExampleConcat() {
363+
s1 := []int{0, 1, 2, 3}
364+
s2 := []int{4, 5, 6}
365+
concat := slices.Concat(s1, s2)
366+
fmt.Println(concat)
367+
// Output:
368+
// [0 1 2 3 4 5 6]
369+
}
370+
371+
func ExampleContains() {
372+
numbers := []int{0, 1, 2, 3}
373+
fmt.Println(slices.Contains(numbers, 2))
374+
fmt.Println(slices.Contains(numbers, 4))
375+
// Output:
376+
// true
377+
// false
378+
}
379+
380+
func ExampleRepeat() {
381+
numbers := []int{0, 1, 2, 3}
382+
repeat := slices.Repeat(numbers, 2)
383+
fmt.Println(repeat)
384+
// Output:
385+
// [0 1 2 3 0 1 2 3]
386+
}

0 commit comments

Comments
 (0)