Func Cap (v Type) int
Returns the capacity of the specified type, depending on the type, with a different meaning.
Array: The number of elements (as with Len (v)).
Array pointer: the number of elements in the *v (as with Len (v)).
Slice:the Maximum length The Slice can reach when resliced; if V==nil, the cap (v) value is 0;
The capacity of the Channel:channel buffer, in which the element is the unit; if V==nil, the cap (v) value is 0;
Reference code:
Package main Import ("FMT") Func main () {arr:= []int{1,2,3}fmt. Println ("Cap (arr):", Cap (arr)) fmt. Println ("Len (arr):", Len (arr))//5 of both length and capacitySlice1:= Make ([]string,5)//length 3 with a capacity of 5Slice2:= Make ([]int,3,5) fmt. Println ("Cap (Slice1):", Cap (Slice1)) fmt. Println ("Cap (SLICE2):", Cap (SLICE2)) C1:= Make (chanint) C2:= Make (chanint,2) fmt. Println ("Cap (C1):", Cap (C1)) FMT. Println ("Cap (C2):", Cap (C2))}
Output Result:
Cap (arr): 3
Len (arr): 3
Cap (SLICE1): 5
Cap (SLICE2): 5
Cap (C1): 0
Cap (C2): 2
Go built-in function cap