cap vs len of slice in golang

GoSlice

Go Problem Overview


What is the difference between cap and len of a slice in golang?

According to definition:

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.

x := make([]int, 0, 5) // len(b)=0, cap(b)=5

Does the len mean non null values only?

Go Solutions


Solution 1 - Go

A slice is an abstraction that uses an array under the covers.

cap tells you the capacity of the underlying array. len tells you how many items are in the array.

The slice abstraction in Go is very nice since it will resize the underlying array for you, plus in Go arrays cannot be resized so slices are almost always used instead.

Example:

s := make([]int, 0, 3)
for i := 0; i < 5; i++ {
	s = append(s, i)
	fmt.Printf("cap %v, len %v, %p\n", cap(s), len(s), s)
}

Will output something like this:

cap 3, len 1, 0x1040e130
cap 3, len 2, 0x1040e130
cap 3, len 3, 0x1040e130
cap 6, len 4, 0x10432220
cap 6, len 5, 0x10432220

As you can see once the capacity is met, append will return a new slice with a larger capacity. On the 4th iteration you will notice a larger capacity and a new pointer address.

Play example

I realize you did not ask about arrays and append but they are pretty foundational in understanding the slice and the reason for the builtins.

Solution 2 - Go

From the source code:

// The len built-in function returns the length of v, according to its type:
//	Array: the number of elements in v.
//	Pointer to array: the number of elements in *v (even if v is nil).
//	Slice, or map: the number of elements in v; if v is nil, len(v) is zero.
//	String: the number of bytes in v.
//	Channel: the number of elements queued (unread) in the channel buffer;
//	if v is nil, len(v) is zero.
func len(v Type) int

// The cap built-in function returns the capacity of v, according to its type:
//	Array: the number of elements in v (same as len(v)).
//	Pointer to array: the number of elements in *v (same as len(v)).
//	Slice: the maximum length the slice can reach when resliced;
//	if v is nil, cap(v) is zero.
//	Channel: the channel buffer capacity, in units of elements;
//	if v is nil, cap(v) is zero.
func cap(v Type) int

Solution 3 - Go

Simple explanation Slice are self growing form of array so there are two main properties.

Length is total no of elements() the slice is having and can be used for looping through the elements we stored in slice. Also when we print the slice all elements till length gets printed.

Capacity is total no elements in underlying array, when you append more elements the length increases till capacity. After that any further append to slice causes the capacity to increase automatically(apprx double) and length by no of elements appended.

The real magic happens when you slice out sub slices from a slice where all the actual read/write happens on the underlaying array. So any change in sub slice will also change data both in original slice and underlying array. Where as any sub slices can have their own length and capacity.

Go through the below program carefully. Its modified version of golang tour example

package main

import "fmt"

func main() {
	sorig := []int{2, 3, 5, 7, 11, 13}
	printSlice(sorig)

	// Slice the slice to give it zero length.
	s := sorig[:0]
	printSlice(s)

	// Extend its length.
	s = s[:4]
	s[2] = 555
	printSlice(s)

	// Drop its first two values.
	s = s[2:]
	printSlice(s)
	
	printSlice(sorig)
}

func printSlice(s []int) {
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)

//Output
//len=6 cap=6 [2 3 5 7 11 13]
//len=0 cap=6 []
//len=4 cap=6 [2 3 555 7]
//len=2 cap=4 [555 7]
//len=6 cap=6 [2 3 555 7 11 13]

Solution 4 - Go

You've answered your question - the length of the underlying array in a slice is not necessarily the same as the number of elements that array contains.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAli AdraviView Question on Stackoverflow
Solution 1 - GojmaloneyView Answer on Stackoverflow
Solution 2 - GoTylerView Answer on Stackoverflow
Solution 3 - GoSumerView Answer on Stackoverflow
Solution 4 - GodaplhoView Answer on Stackoverflow