This is a creation in Article, where the information may have evolved or changed.
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, ten)
len (Slice1) = 5, Cap (SLICE1) = 10, the initial value of the element is 0
2.2 related Letter Number
len (slice): Returns the number of elements in slice (length)
cap (slice): return slice Allocation space size
Append (Slice1, Slice2 ...): Append slice2 to Slice1 to generate a new slice, if SLICE2 is a variable, you cannot omit it. ., equivalent to append (Slice1, a[, B, ...])
copy (target slice, source slice): Copies the source slice to the target slice
2.3 cap-l, whichever is the minimum number of slice elements En = 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 as array
2.5 When passing parameters, the pointer is passed /p>