Go arrays and slices

Source: Internet
Author: User

Arrays Array
To define the format of an array: var <varname>[n]<type>,n>0
Array lengths are also part of a type, so arrays with different lengths are of different types,
Different types cannot be assigned to each other
Note A pointer and pointer array that points to an array
Array is a value type in Go
Arrays (the same type) can be compared using = = or! =, but cannot use > or <
You can use new to create an array, and this method returns a pointer to the array
Go supports multidimensional arrays

Func Main () {a:=[3]int{2:1,1:3} B:=[...]int{1:1,2:2,5:5}    varD |2]int= [2]int{1,3} fmt. Println (a)//[0 3 1]Fmt. Println (b)//[0 1 2 0 0 5]Fmt. Println (c)//[1 3]}
Func Main () {varD |2]int= [2]int{1,3}    varP *[2]int=&C FMT. PRINTLN (P)//Pointer to array &[1 3]x,y:=1,2A:=[2] *int{&x,&y} fmt. Println (a)//[0xc0820022a0 0xc0820022a8] pointer array}
Func Main () {    A:=[int{}    a[1]=2    FMT. Println (a)  //[0 2 0 0 0 0 0 0 0 0]    p:=new([int] c17>)    p[1]=2    //&[0 2 0 0 0 0 0 0 0 0]}

Go multidimensional array

Func Main () {    A:= [2] [3]int{            {111 },            {222}}     fmt. Println (a)}

Go language Bubble sort

Func Main () {arr:=[...]int{3,1,8,2,7} fmt. Println (arr) Len:=Len (arr) Count:=0  //Number of exchanges     fori:=0; i<len;i++{         forj:=i+1; j<len;j++{            ifArr[i]>arr[j]{tmp:=Arr[j] Arr[j]=Arr[i] Arr[i]=tmp Count++}}} fmt. Println (arr) fmt. Println (count)//4}

  for  I:=0 ; I<3 ; I++{V:  =1   FMT. Println ( &v)  each time a new address  }} in the outer loop V:  =1  v:  =1   is wrong  

slice slice
  itself is not an array, it points to the underlying array
Span style= "color: #339966;" >  as an alternative to a variable-length array, you can associate a local or all
  as a reference type
  can be created directly or get a build from the underlying array
  uses Len () to get the number of elements, the cap () gets the capacity
  generally uses make () to create
  if multiple slice point to the same underlying array, one value change affects all

Make ([]type, Len,cap)
Where the cap can be omitted, omitted and the Len value is the same
Len represents the number of stores, and the CAP represents the capacity

Func Main () {varT |Ten]int=[Ten]int{1,2,3,4,5,6,7,8,9,Ten} fmt. Println (s)//S1:=s[5:10]//Front closed Post index contains 5 not 10, from index 5 to last 3 form//S1:=s[5:len (s)]s1:=s[5:] S2:=s[:5] FMT. Println (S1)//[6 7 8 9]Fmt. PRINTLN (S2)//[1 2 3 4 5]}

Func Main () {    S1:=make ([]int,3,ten)    FMT. Println (Len (S1), Cap (S1))//3    FMT. Println (S1)//[0 0 0]}

Reslice
Reslice indexes are subject to slice slices
The index may not exceed the cap () value of the capacity of the slice slice//exceeds the memory block
Index out-of-bounds does not cause redistribution of the underlying array but throws an error

Func Main () {a:=[]string{"a","b","C","D","e","F","g","h","I","J"} S1:=a[2:5] FMT. Println (S1)//[C d e]Fmt. Println (Len (S1), Cap (S1))//3 8 points to 1 contiguous blocks of memorys2:=s1[:2] FMT. PRINTLN (S2)//[C D]Fmt. Println (len (S2), Cap (S2))//2 8s3:=s2[1:] FMT. Println (S3)//[d]Fmt. Println (Len (S3), Cap (S3))//1 7}

Append
You can append elements to the slice tail
One slice can be appended to the tail of another slice
Returns the original slice if the final length does not exceed the capacity appended to the slice
If the capacity of the appended slice is exceeded, the array is reassigned and the original data is copied

UNC Main () {s1:=make ([]int,3,6) fmt. Printf ("%p\r\n", S1)//0xc082005da0S1=append (S1,1,2,3) fmt. Printf ("%v,%p\r\n", S1,S1)//[0 0 0 1 2 3],0xc082005da0S1=append (S1,4,5,6) fmt. Printf ("%v,%p", S1,S1)//[0 0 0 1 2 3 4 5 6],0xc08203a120 (address changed, reassigned)}

Func Main () {a:=[]int{1,2,3,4,5} S1:=a[2:5]//[3 4 5]s2:=a[1:3]//[2 3]FMT. Println (S1,S2) s1[0]=9FMT. Println (s1,s2,a)//[ 9 4 5] [2 9] [1 2 9 4 5]a[1]=7FMT. Println (s2,a)//[7 9] [1 7 9 4 5]}
Func Main () {a:=[]int{1,2,3,4,5} S1:=a[2:5]//[3 4 5]s2:=a[1:3]//[2 3]FMT. Println (S1,S2) S2=append (S2,1,1,1,1,1,1,1,1,1,1) s1[0]=9FMT. Println (s1,s2,a)//[ 9 4 5] [2 3 1 1 1 1 1 1 1 1 1 1] [1 2 9 4 5]//S2 Append after the length exceeds the capacity to obtain a new address no longer points to the a,s1 the change does not affect it}

Copy

Func Main () {s1:=[]int{1,2,3,4,5,6} S2:=[]int{7,8,9}//copy (S1,S2)//Copy the S2 into the S1.//FMT. Println (S1,S2)//[7 8 9 4 5 6] [7 8 9]//copy (S2,S1)//FMT. PRINTLN (S2)//[1 2 3]Copy (s2[2:],S1) fmt. PRINTLN (S2)//[7 8 1]}

Go arrays and slices

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.