Go language slice length and capacity

Source: Internet
Author: User
Package Mainimport "FMT" Func main () {s: = []int{2, 3, 5, 7, one, 13}printslice (s)//Slice the Slice to give it zero length. s = S[:0]printslice (s)//Extend its length.fmt.Println (s[:5]) s = s[:4]printslice (s)//Drop its first of VALUES.S = S[2:]p Rintslice (s)}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 13]len=0 cap=6 [][2 3 5 7 11]len=4 cap=6 [2 3 5 7]len=2 cap=4 [5 7]

  

The first re-assignment of s to the underlying array of 0 the   second s or 0 points  to the third point 2 everything starts with the one pointing to the index start slice of the cap that is the current slice of 0 pointing to the start to end

  

If you don't understand, you can see.

Go language slice length and capacity

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.