Built-in function

Go cap()

Reports array length, pointed-to array length, slice capacity or channel buffer capacity. Nil slices and channels have capacity zero. For slices, capacity is the number of elements available from the slice start to the end of its underlying array. Example prints 5.

Formal Syntax & Structure

go syntaxgo
cap(v)

Parameters & Attributes

NameTypeDescription
varray, array pointer, slice or channelValue whose capacity is queried.

Return Value

int

Capacity; possibly a constant for arrays.

Working Example

Example Snippetgo
package main
import "fmt"
func main() {  s := make([]int, 2, 5)  fmt.Println(cap(s))}
Go Reference