March 2019
Intermediate to advanced
336 pages
9h 9m
English
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.
Read now
Unlock full access