This is a creation in Article, where the information may have evolved or changed.
Array Arrays
An array is a built-in (build-in) type, a collection of data of the same type, which is a value type that accesses element values through a subscript index starting at 0. The length is fixed after initialization, and its length cannot be modified. When a parameter is passed in as a method, an array is copied instead of referencing the same pointer. The length of the array is also part of its type, and its length is obtained through the built-in function len (array).
Initialization
Array initialization in several forms, view sample code, run sample code online
[5] int {1,2,3,4,5}
An array of length 5 whose element values are: 1,2,3,4,5
[5] int {1,2}
An array of length 5 whose element values are: 1,2,0,0,0. An element that does not specify an initial value at initialization will be assigned a value of the default for its element type int 0,string The default value is ""
[...] int {1,2,3,4,5}
An array of length 5, whose length is determined by the number of elements specified at initialization
[5] int { 2:1,3:2,4:3}
An array of length 5, Key:value, whose element values are: 0,0,1,2,3. The corresponding value in the 2,3,4 index was specified during initialization:
[...] int {2:1,4:3}
An array of length 5, starting with the element value: 0,0,1,0,3. Because a value of 3 is specified for the maximum index of 4, the length of the initialized element is determined to be 5
Assignment and use
Arrays access elements by subscript, modifying their element values
arr :=[...] int {1,2,3,4,5}arr[4]=arr[1]+len(arr) //arr[4]=2+5
To iterate through the array elements, view the sample code, run the sample code online
arr := [5]int{5, 4, 3}for index, value := range arr { fmt.Printf("arr[%d]=%d \n", index, value)}for index := 0; index < len(arr); index++ { fmt.Printf("arr[%d]=%d \n", index, arr[index])}
An array is a value type, and assigning an array to another array will copy a new element, view the sample code, run the sample code online
arr2 := [5]int{1, 2} arr5 := arr2arr5[0] = 5arr2[4] = 2fmt.Printf(" arr5= %d \n arr2=%d \n arr5[0]==arr2[0]= %s \n", arr5, arr2, arr5[0] == arr2[0])OutPut: arr5=[5 2 0 0 0] arr2=[1 2 0 0 2] arr5[0]==arr2[0]= false
Sliced Slices
The length of the array can not be changed, in a particular scenario such a collection is not very suitable, go provides a flexible, powerful built-in type Slices slices, compared to the array of the length of the slice is not fixed, the addition of elements, may increase the capacity of the slice. There are two concepts in a slice: len length , and cap capacity , which is the maximum subscript +1 that has been assigned a value, which can be obtained through the built-in function Len (). Capacity refers to the maximum number of elements that a slice can currently hold, which can be obtained through the built-in function cap (). A slice is a reference type, so the same pointer will be referenced when the slice is passed, and modifying the value will affect other objects.
Initialization
Slices can be initialized by an array, or by using the built-in function make (). Len=cap at initialization time, if the capacity cap is not sufficient when appending elements to see sample code at 2 times the size of Len, run the sample code online
s :=[] int {1,2,3 }
The direct initialization of the slice,[] means the slice type, and the {-I} initialization value is in turn a. Its cap=len=3
s := arr[:]
Initializes the slice S, which is a reference to the array arr
s := arr[startIndex:endIndex]
Create an element in Arr from subscript startindex to endIndex-1 as a new slice
s := arr[startIndex:]
The default endindex will represent the last element to arr
s := arr[:endIndex]
The default startindex will represent the start of the first element in arr
s1 := s[startIndex:endIndex]
Initializing slices by slicing s S1
s :=make([]int,len,cap)
Initialize slice s,[]int with built-in function make () to identify slices whose element type is int
Assignment and use
A slice is a reference type and you need to be aware of its actions when you use it. To view the sample code, run the sample code slice online via the built-in function append (slice []type,elems ... Type) appends an element, Elems can be a row of type data, or it can be slice because one of the elements is appended, so if you append one slice to another slice you need to bring "...", This means that the elements in the slice are appended to the other slice in turn. In addition, the subscript cannot exceed the Len size when the element is accessed by subscript, as if the subscript of the array cannot exceed the Len range.
-
S: =append (s,1,2,3,4)
-
S: =append (s,s1 ...)