上QQ阅读APP看书,第一时间看更新
The len function
The len() function gives the current length of slice, and the capacity of slice can be obtained using the cap() function. The following code sample shows the basic slice creation and appending a slice (basic_slice.go):
var slice = []int{1,3,5,6}
slice = append(slice, 8)
fmt.Println(“Capacity”, cap(slice))
fmt.Println(“Length”, len(slice))
Run the following command to execute the preceding code:
go run basic_slice.go
The following screenshot displays the output:
Let's take a look at the slice function in the next section.