Go array, slice

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

Array


Package Mainimport"FMT"Func Main () {/* Declaration of Array *1, Var ab [2]int2, var BC3, CD: = [2]int{} [3]int{1,2}    [5]int{4:1}    [...]int{4:1}    [...]int{4,1,3}    [...]int{0:4,1:1,2:3} */Var a [2]string FMT. Println (a) var ab [2]int = [2]int{} fmt. PRINTLN (ab) var BC = [2]int{1,2} FMT. PRINTLN (BC) c: = [3]int{} fmt. Println (c) B: = [3]int{1,2} FMT. Println (b) E: = [5]int{4:1} FMT. Println (e) G: = [...]int{4:1} FMT. Println (g) F: = [...]int{4,1,3} FMT. Println (f) H: = [...]int{0:4,1:1,2:3} FMT. Println (h) hh: = [...]int{0:4,1,2} FMT. PRINTLN (HH)//array pointer fmt. Println ("---------array pointer----------") I: = [...]int{9:1} var p *[Ten]int = &i FMT. PRINTLN (p) fmt. PRINTLN (*p) x, y: =1,2J: = [...]*int{&x, &y} fmt. Println (j) K: = [2]int{1,2} L: = [2]int{1,2} FMT. Println (k = = l)//Pointer to array m: = new ([5]int) fmt. PRINTLN (m)//operation of an element AA: = [Ten]int{} aa[1] =2Fmt. PRINTLN (aa) BB: = new ([Ten]int) bb[1] =2Fmt. Println (BB) cc: = [2][3]int{{1,1,1},        {2,2,2}} FMT. PRINTLN (cc) DD: = [...][3]int{{1,1,1},        {2,2,2}} FMT. PRINTLN (dd) FF: = [...][3]int{{1,1},        {0:2,2,2}} FMT. PRINTLN (FF)//The following is the wrong way//EE: = [...][...]int{//{1,1,1},    //      {2,2,2}}//FMT. PRINTLN (EE)}
[ ][0 0][1 2][0 0 0][1 2 0][0 0 0 0 1][0 0 0 0 1][4 1 3][4 1 3][4 1 2]---------Array pointer----------&[0 0 0 0 0 0 0 0 0 1][0 0 0 0 0 0 0 0 0 1][0xc0820083a0 0xc0820083a8]true&[0 0 0 0 0][0 2 0 0 0 0 0 0 0 0]&[0 2 0 0 0 0 0 0 0 0][ [1 1 1] [2 2 2]][ [1 1 1] [2 2 2]][ [1 1 0] [2 2 2]]



Slice

这里写代码片
Package Mainimport"FMT"Func Main () {/ * Declaration 1, A1: = 2, var b []int 3, var d []int{1, 3} []int {}    */A: = []int{} fmt. Println (a) A1: = []int{1,3} FMT. PRINTLN (A1)varb []intFmt. Println (b)varc []int= []int{} fmt. Println (c)vard = []int{} fmt. Println (d)//With head not including tailFmt. Println ("--------intercept------------") E: = [5]int{1,2,3,4,5} F: = e[2:4] G: = e[:2] H: = e[2] I: = e[2:] FMT. Println (i) fmt. Println (h) fmt. Println (g) fmt. Println (f)//3 elements 10 memoryFmt. Println ("--------make------------") J: = Make ([]int,3,Ten) FMT. Println (j)//length, Maximum capacityFmt. Println (Len (j), Cap (j)) M: = []byte{' A ',' B ',' C ',' d ',' E ',' F ',' G ',' h '} N: = m[2:5] FMT. Println (string(n) fmt. Println (Len (n), Cap (n)) Q: = n[3:5] FMT. Println (string(q))//appendS1: = Make ([]int,3,6) FMT. Printf ("%p\n", S1)//memory addressS1 = append (S1,1,2) FMT. Printf ("%v%p\n", S1, s1) S1 = append (S1,1,2,3)//exceeds the original length, the memory will be redistributedFmt. Printf ("%v%p\n", S1, S1)//points to memory addressFmt. Println ("-----memory address----") LL: = []int{1,2,3,4,5,6} LL1: = ll[2:5]//345LL2: = ll[1:3]//23Fmt. Println (LL1, LL2) ll1[0] =9Fmt. Println (LL1, LL2)/////////////////////////// ///MM: = []int{1,2,3,4,5,6} MM1: = mm[2:5]//345mm2: = mm[1:3]//23Fmt. Println (MM1, mm2) mm2 = append (mm2,1,2,3,4,5) mm1[0] =9Fmt. Println (MM1, mm2)/  ////////copy/////////// /////O1: = []int{1,2,3,4} O2: = []int{5,6} copy (O1, O2)//o2 Copy to O1Fmt. Println (O1, O2) Oo1: = []int{1,2,3,4} Oo2: = []int{5,6} copy (Oo2, oo1)//oo1 Copy to Oo2Fmt. Println (Oo1, oo2) Ooo1: = []int{1,2,3,4} Ooo2: = []int{5,6,7} copy (ooo1[2:4], ooo2[0:2])//oo1 Copy to Oo2Fmt. Println (OOO1)}
[][1 3][][][]--------Intercept------------[3 4 5]3[1 2][3 4]--------make------------[0 0 0]3 10CDE3 6FG0xc0820341b0[0 0 0 1 2]0xc0820341b0[0 0 0 1 2 1 2 3]0xc08202e180-----Memory address----[3 4 5] [2 3][9 4 5] [2 9][3 4 5] [2 3][9 4 5] [2 3 1 2 3 4 5][5 6 3 4] [5 6][1 2 3 4] [1 2][1 2 5 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.