Go built-in function cap

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.