-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice-len-cap.go
More file actions
45 lines (35 loc) · 883 Bytes
/
Copy pathslice-len-cap.go
File metadata and controls
45 lines (35 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Run this example from terminal by
* using this command - go run slice-len-cap.go
*
* Output:
len=6 cap=6 [2 3 5 7 11 13]
len=0 cap=6 []
len=4 cap=6 [2 3 5 7]
len=2 cap=4 [5 7]
* Document
A slice has both a length and a capacity.
The length of a slice is the number of elements it contains.
The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.
The length and capacity of a slice s can be obtained using the expressions len(s) and cap(s).
*/
package main
import (
"fmt"
)
func main() {
s := []int{2, 3, 5, 7, 11, 13}
PrintSlice(s)
// Slice the slice to give it zero length.
s = s[:0]
PrintSlice(s)
// Extend It's Length
s = s[:4]
PrintSlice(s)
// Drop It's first two values
s = s[2:]
PrintSlice(s)
}
func PrintSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}