Slices of the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
package mainimport  ("FMT") Func main ()  {arr := [...] string{"Go",  "python",  "java",  "C + +",  "C"}fmt. Println ("arr value ",  arr) fmt. Println ("------------------------") slice1 := arr[1:3]fmt. Println ("slice1 now is ",  slice1) fmt. Println ("len of slice1 is ",  len (Slice1),  ", Cap of sclie1 is   ",  cap (Slice1)) fmt. Println ("------------------------") slice2 := append (slice1,  "Ruby",  "Obj-c")  // If you add another PHP, it will be a completely different picture ... Fmt. Println ("slice2 is ",  slice2) fmt. Println ("len of slice2 is ",  len (Slice2),  ", Cap of sclie2 is   ",  cap (Slice2)) fmt. Println ("after append : slice1 is ",  slice1)  //The underlying array, though changed, But Slice1 Len did not change the FMT. Println ("len of slice1 is ",  len (Slice1),  ", Cap of sclie1 is   ", &nbsP;cap (Slice1)) fmt. Println ("after append : arr value is ",  arr) fmt. Println ("------------------------") Slice1 = slice1[:cap (Slice1)] //this time is right ... Fmt. Println ("after reslice : slice1 is ",  slice1) fmt. Println ("len of slice1 is ",  len (Slice1),  ", Cap of sclie1 is   ",  cap (Slice1))}

Output Result:

arr value  [go python java  C++ C]------------------------slice1 now is  [python java]len of  slice1 is  2 ,cap of sclie1 is  4------------------------ Slice2 is  [python java ruby obj-c]len of slice2 is  4  ,cap of sclie2 is  4after append : slice1 is  [ python java]len of slice1 is  2 ,cap of sclie1 is   4after append : arr value is  [go python java ruby  OBJ-C]------------------------After reslice : slice1 is  [python java  ruby obj-c]len of slice1 is  4 ,cap of sclie1 is   4 


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.