Volume and length of Go slice

Source: Internet
Author: User
Package Mainimport (  "FMT") func main () {  A: = []int{1,2,3,4}  FMT. Println ("A:", Len (a), cap (a), a)  b: = [10]int{1,2,3,4}  FMT. Println ("B:", Len (b), Cap (b), B)  c: = make ([]int, 4, ten)  FMT. Println ("C:", Len (c), Cap (c), C)  D: = B[:5]  fmt. Println ("D:", Len (d), Cap (d), D)  e: = Append (d,5)//append after the capacity of D is unchanged  e[0] = 100//does not exceed the capacity of the underlying array, so both E and D point to the same array, Modifying e affects the D  fmt. Println ("D after append:", Len (d), Cap (d), D)  FMT. Println ("E:", Len (e), Cap (e), E)}

Execution Result:

A:4 4 [1 2 3 4]

B:10 10 [1 2 3 4 0 0 0 0 0 0]

C:4 10 [0 0 0 0]

D:5 10 [1 2 3 4 0]

D after Append:5 [2 3 4 0]

E:6 [2 3 4 0 5]

Volume and length of Go slice

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.