Go Slice Reference type description

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Today's no-smell go into the basics, see the slice as a reference type of description, write code experiment a bit

    
    varSlice1[]int=make ([]int,  4,6)
  var    slice2  []int    =  Slice1 
  FMT. Println (Slice1,  slice2)   //    output    Span style= "COLOR: #0800" >[0    0    0]    [0    0    0]  
    Operation Slice1& slice2  See if it affects each other  .  
    slice1[1]= One
    slice2[2]=
  slice1  =  append  (Slice1,   33 ) 
  FMT. Println (Slice1,  slice2)   //  output result  [0    11    22    0    33]    [0    11    22    0]  
  slice2  =  append  (Slice2,   44 )   Span style= "COLOR: #0800" >// 
    Fmt. Println (Slice1,slice2)[022   0 [0]      0]

View output: The reference type of the discovered slice is said to be an assignment, passing in a parameter, only sharing the underlying array, and does not share the quantity variable of two slices

varSlice []int = make([] int , 6,ten)

 fmt. Println(slice)[0000  00]   
 fillslice(slice)
    Fmt.Println(Slice)// [0   1    2   3  0 0]/ *   
 Populating the contents of a slice
*/
Funcfillslice (slice[]int){
 Slice[0]=0
 Slice[1]=1
 Slice[2]=2
 Slice[3]=3
 Slice=append(slice,4 )
 Slice=Append (Slice, 5) 
 Slice=append(slice ,  6 )
}

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.