1. Array
A collection of data of the same type
var arr [n]type//Declaration type type one-dimensional array
var arr [m][n]type//Declare type two-dimensional array
multidimensional arrays and so on
can also be used: = declaration
Arr: = [n]type{element 1[, Element 2, ...]} where n can be used "..." Three points indicate that the system determines the number of elements
Subscript can only be of type int, and PHP supports string type subscript
1.1 Number of team leader Len (arr)
Note: The array length is not variable after it is defined
1.2 Traversal:
A.looping through an array of subscripts access Arr[0] ~ arr[(len)]
b. Range arr, with two return values the first array subscript, and the second as the value of the element, similar to a PHP traversal array
[Plain]View Plaincopyprint?
- For k, V: = range Array {
- Fmt. Printf ("arr[%d] =%d \ t", K, V)
- }
[PHP]View Plaincopyprint?
- foreach ($arr as $k = = $v) {
- printf ("arr[%d] =%d \ t", $k, $v);
- //echo ' $arr ['. $k. "] = " . $v. "\ t";
- }
1.3 When an array is assigned and passed a parameter, it produces an array copy instead of using its pointer
2. Slice
When an array is defined, its length is fixed, and the array is a value type
And slice is a mutable array, but a reference type
2.1 Three ways to generate slice
A.the declaration is the same as an array, but does not require a specified length
var Slice1 []int
Slice2: = []int {element 1[, Element 2, ...]}
b. Get arr[i:j] from an array (or slice or string) i= array start position, j= end bit result, j-i= slice length, I and J can be omitted, omitted i=0, J=len (arr), I is starting from 0, J is starting from 1
A b c d E F
I 0 1 2 3 4 5
J 1 2 3 4 5 6
[Plain]View Plaincopyprint?
- slice1 := arr[:] //arr[0:6]/arr[0:]
- slice2 := arr[1:1] //[]
- SLICE4 := ARR3 [1:3] //b c
- Slice5 := arr3[:5] // = arr3[0:5]
c. Make
Slice1: = Make ([]int, 5, 10)
Len (Slice1) = 5, Cap (SLICE1) = 10, the initial value of the element is 0
2.2 Correlation functions
Len(Slice): Number of elements returning slice (length)
Cap (slice): Returns the allocated space size of the slice
Append (Slice1, Slice2 ...): Append slice2 to Slice1 to generate new slice, if SLICE2 is variable, cannot omit ..., equivalent to append (Slice1, a[, B, ...])
Copy (Target slice, source slice): Copies the source slice to the target slice, whichever is the smallest number of slice elements
2.3 Cap-len = 0 o'clock, the system will dynamically allocate new array space, that is, the slice will automatically handle the problem of insufficient storage space
2.4 Traversal is the same as array
2.5 passed as a pointer when parameters are passed
"Go" Golang slice array