GO language summary (3) - and an array of slices

  On Bowen brief introduction about the basic types of Go language - GO language summary (2) - basic types , this blog began to introduce an array of slices and Go language.

First, the array 

  Similar to most other languages, the same array Go language is also an element type of fixed-length sequences.

(1) create the array.

  There are three ways to create the array: [length] the Type , [N] {the Type VALUE1, value2, ..., valueN} , [...] the Type {VALUE1, value2, ..., valueN} as follows:

func test5() {
    var iarray1 [5]int32
    var iarray2 [5]int32 = [5]int32{1, 2, 3, 4, 5}
    Iarray3: = [ 5 ] Int32 { 1 , 2 , 3 , 4 , 5 }
    Iarray4: = [ 5 ] {Int32 6 , 7 , 8 , 9 , 10 }
    iarray5 := [...]int32{11, 12, 13, 14, 15}
    iarray6 := [4][4]int32{{1}, {1, 2}, {1, 2, 3}}
    fmt.Println(iarray1)
    fmt.Println(iarray2)
    fmt.Println(iarray3)
    fmt.Println(iarray4)
    fmt.Println(iarray5)
    fmt.Println(iarray6)
}

  result:

[0 0 0 0 0]
[1 2 3 4 5]
[1 2 3 4 5]
[6 7 8 9 10]
[11 12 13 14 15]
[[1 0 0 0] [1 2 0 0] [1 2 3 0] [0 0 0 0]]

We see an array iarray1, only a statement, not an assignment, Go language to help us automatically assigned to 0. Look iarray2 and iarray3, we can see the statement in Go, the type may indicate or may not indicate the type, var iarray3 = [5] int32 {1, 2, 3, 4, 5} is not in question.

 

Capacity and length (2) of the array is the same. CAP () function and len () function are the output capacity of the array (i.e., length). Such as:

test6 function () {
    Iarray4: = [ 5 ] {Int32 6 , 7 , 8 , 9 , 10 }
    fmt.Println(len(iarray4))
    fmt.Println(cap(iarray4))
}

Output is 5.

 

(3) Use:

test7 function () {
    iarray7: = [ . 5 ] String { " AAA " , `bb`, " can be friends " , " call me what to say " , " () " }
    fmt.Println(iarray7)
    for i := range iarray7 {
        fmt.Println(iarray7[i])
    }
}

Output:

test7 function () {
    iarray7: = [ . 5 ] String { " AAA " , `bb`, " can be friends " , " call me what to say " , " () " }
    fmt.Println(iarray7)
    for i := range iarray7 {
        fmt.Println(iarray7[i])
    }
}

 

 

Second, sliced

  Go language, the slice is variable-length, fixed-capacity identical sequence of elements. Slice the nature of the Go language is an array. Because a fixed length array capacity is fixed, i.e. the capacity of the hidden sections of the length of the array. It refers to a variable length in the range of the variable length of the array.

 

(1) create a slice.

  Create a slice of 4 ways:

  1)make ( []Type ,length, capacity )

  2)  make ( []Type, length)

  3) []Type{}

  4) [] Type {value1, value2, ..., valueN} 

From 3), 4) visible, creating tiles create an array with the only difference is whether there is a digital pre Type "[]" in is empty, on behalf of slices, or on behalf of the array. Because the slice is variable length. The following is an example to create a slice:

test8 function () {
    slice1 := make([]int32, 5, 8)
    slice2 := make([]int32, 9)
    Slice3: = [] {} Int32
    slice4 := []int32{1, 2, 3, 4, 5}
    fmt.Println(slice1)
    fmt.Println(slice2)
    fmt.Println(slice3)
    fmt.Println(slice4)
}

The output is:

[0 0 0 0 0]
[0 0 0 0 0 0 0 0 0]
[]
[1 2 3 4 5]

 As above, to create four sections, three empty slices, a slice value.

 

(2) sections and hiding array:

A slice is a hidden reference to the array, and also refer to the same array to slice the slices. As an example, to create a slice slice0, and creates two sections slice1 slice2 and based on this slice:

test9 function () {
    slice0 := []string{"a", "b", "c", "d", "e"}
    slice1: = slice0 [ 2 : Only (slice0) - 1 ]
    slice2: = slice0 [: 3 ]
    fmt.Println(slice0, slice1, slice2)
    slice2[2] = "8"
    fmt.Println(slice0, slice1, slice2)
}

The output is:

[a b c d e] [c d] [a b c]
[a b 8 d e] [8 d] [a b 8]

Visible, slice slice0, slice1 is slice2 and refer to the same underlying array so slice2 changed, the other two will change.

 

(3) traversal, amend sections:

test10 function () {
    slice0 := []string{"a", "b", "c", "d", "e"}
    fmt.Println ( " \ n-traversing element ~~~~~~ ~~~~~~ " )
     for _, ELE: = Range slice0 {
        fmt.Print(ele, " ")
        ele = "7"
    }
    fmt.Println ( " \ n-indexing through ~~~~~~ ~~~~~~ " )
     for index: = Range slice0 {
        fmt.Print(slice0[index], " ")
    }
    fmt.Println ( " \ n-element index commonly used ~~~~~~ ~~~~~~ " )
     for index, ELE: = Range slice0 {
        fmt.Print(ele, slice0[index], " ")
    }
    fmt.Println("\n~~~~~~修改~~~~~~")
    for index := range slice0 {
        slice0[index] = "9"
    }
    fmt.Println(slice0)
}

As the first three different recycling loop for Range, when there are two elements for the back, front Range, index representative of the first element, the second element represents the value of the element using a "_" indicates ignored, because go language, the value of the unused causes a compilation error.

Is only one element that represents the index.

Only use indexes to modify elements. As in the first traversal, the assignment ele 7, no effect results. Because the element traversal, ele is passed by value, ele is a copy of the slice elements, modify it does not affect the original value, while in the fourth traversal - traversal of the index, the modification is the value of the slice element references, so You can modify.

The results are:

~~~~~~ ~~~~~~ traversing element
a b c d e 
~~~~~~ indexing through ~~~~~~
a b c d e 
~~~~~~ ~~~~~~ use common element index
aa bb cc dd ee 
~~~~~~ ~~~~~~ modification 
[ . 9  . 9  . 9  . 9  . 9 ]

 

(4), an additional copy sections:

test11 function () {
    slice := []int32{}
    fmt.Printf ( " length of the slice is:% d, slice is: V% \ n- " , len (slice), slice)
    slice = append(slice, 12, 11, 10, 9)
    fmt.Printf ( " after adding the length of the slice is:% d, slice is: V% \ n- " , len (slice), slice)
    slicecp := make([]int32, (len(slice)))
    fmt.Printf ( " length of slicecp:% d, slicecp is: V% \ n- " , len (slicecp), slicecp)
    copy(slicecp, slice)
    fmt.Printf ( " after copying assignment is slicecp length:% d, slicecp is: V% \ n- " , len (slicecp), slicecp)
}

Adding, copying slices, using a built-in functions and append copy, copy function returns the number of the last copied elements.

 

(5), built-in functions append

Append built-in functions may be added one or more other types of values ​​to the same slice. If the number of elements in additional capacity over the original slices, then finally returns a new array of new sections. If not, then the final return of the original array of new sections. In any case, append no impact on the original slices. The following example:

test12 function () {
    slice := []int32{1, 2, 3, 4, 5, 6}
    slice2 := slice[:2]
    _ = append(slice2, 50, 60, 70, 80, 90)
    fmt.Printf("slice为:%v\n", slice)
    fmt.Printf ( " slicing operation: V% \ n- " , slice2)
    _ = append(slice2, 50, 60)
    fmt.Printf("slice为:%v\n", slice)
    fmt.Printf ( " slicing operation: V% \ n- " , slice2)
}

As described above, the method append 2 times and the results returned result is completely different, because the number of elements in the second method append additional capacity does not exceed the slice. And no matter what, the original slice slice2 have no effect. result:

slice is: [ . 1  2  . 3  . 4  . 5  . 6 ]
Slicing operation: [ . 1  2 ]
slice is: [ . 1  2  50  60  . 5  . 6 ]
Slicing operation: [ . 1  2 ]

 

 

Reference: "Go Programming Language"

Reproduced in: https: //www.cnblogs.com/zrtqsk/p/4148495.html

Guess you like

Origin blog.csdn.net/weixin_33969116/article/details/93248549