This is a creation in Article, where the information may have evolved or changed. The
Go language supports composite types:
- Arrays: Array
- Slices: Slice
- Pointer: pointer
- Dictionary: Map
- Channel: Chan
- struct: struct
- Interface: interface
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. Loop 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
For k, V: = range array { FMT. Printf ("arr[%d] =%d \ t", K, V) }
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. Declaration is the same as array, but does not require a specified length
var Slice1 []int
Slice2: = []int {element 1[, Element 2, ...]}
b. Get the starting position of the Arr[i:j] i= array from an array (or slice or string), 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
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 returned 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
3. Map
Available in languages such as Java/python
It can be understood for the time being: an unordered table
Its length is also not fixed, it is a reference type, in the assignment and the parameter process, point to the same address
Key1 value1
Key2 value2
... ...
3.1 Declaration and Creation
var map variable name map[key Type] value type//key type can be int or string, value type, need make initialization
Map variable name = make (map[key type] value type)
Or
Map variable Name: = Make (map[key type] value type) [{Key 1: Value 1}[,{Key 2: Value 2},...}]//[] is optional
3.2 Related Operations
Assignment: Map variable name [key] = value
READ: Map variable name [key]//If the key does not exist, return nil
Delete key-value pairs: Delete (map variable name, key)//If key does not exist, no effect
Number of elements to take map: Len (map variable name)
Cannot use the CAP function
Note_type_2.go code List
Package Mainimport "FMT" Func Main () {//First declared after assignment var arr [3]int//arr = {1, 2, 3}//cannot be assigned to array arr[0] = 1arr[1] = 2arr[2] = 3//ar R[3] = 4//compile error:index out of boundsfmt. Println (Arr[0], arr[0:2])//Output 1 [1 2]//arr1: = [3]int{4, 5, 6}arr1: = [...] Int{4, 5, 6}//And the above results as FMT. Printf ("arr[1] =%d \ n", arr1[1])//output ARR[1] = 5//traversal array for I: = 0; I < Len (arr); i++ {fmt. Printf ("arr[%d] =%d \ t", I, Arr[i])}fmt. Println () for k, V: = Range arr1 {fmt. Printf ("arr[%d] =%d \ t", K, v)}fmt. Println () changevalue (arr1) fmt. PRINTLN ("Result:", arr1)//result: [4 5 6] value does not change//two-dimensional array arr2: = [2][3]int{{1, 2}, {4, 5, 6}}fmt. Println ("len (arr2) =", Len (ARR2))//len (ARR2) = 2fmt. Println ("Len (arr2[0]) =", Len (Arr2[0]))//len (arr2[0]) = 3for I, V: = Range arr2 {fmt. Println (i, v)}for i1, v1: = Range arr2 {for I2, V2: = Range V1 {fmt. Printf ("arr2[%d][%d] =%d \ t", I1, I2, v2)}fmt. Println ()}//sliceslice1: = []byte {' B ', ' B '}slice1[0] = ' a '//if only slice is declared, no element, direct assignment by subscript compile errorfmt. Printf ("slice1[0] =%c \ n ", slice1[0])//slice1[0] = aarr3: = [...] byte {' A ', ' B ', ' C ', ' d ', ' e ', ' f '}slice2: = arr3[:]//= Arr3[0:]fmt. Println ("Slice2 =", Slice2) Slice3: = arr3[1:1]//Empty FMT. Println ("Slice3 =", slice3) Slice4: = Arr3[1:3]//b cfmt. Println ("Slice4 =", Slice4) Slice5: = Arr3[:5]//= Arr3[0:5]fmt. Println ("Slice5 =", Slice5) Slice6: = Slice5[1:2]//bfmt. Println ("Slice6 =", slice6) str: = "Hello" slice7: = Str[4:5]//ofmt. Printf ("slice7[0] =%s \ n", Slice7)//slice7[0] = ofmt. Println ("len (Slice4) =", Len (Slice4))//len (Slice4) = 2fmt. Println ("cap (Slice4) =", Cap (Slice4))//cap (Slice4) = 5//slice8: = Append (Slice4, ' a ', ' d ')//b c a dslice9: = []by Te {' A ', ' B '}slice8: = Append (Slice4, Slice9 ...) b c A d = append (Slice4, ' A ', ' B ') fmt. Printf ("slice8[2] =%c \", slice8[2])//slice8[2] = afmt. Println ("len (Slice8) =", Len (Slice8))//len (Slice8) = 4slice10: = []byte {' x ', ' y ', ' z '}copy (slice10, Slice9) for I, V: = Range Slice10 {fmt. Printf ("slice10[%d] =%c \ t", I, v)//a b z}Fmt. Println ()/*//slice9 = A, B; Slice10 = x, y,z copy (Slice9, slice10) for I, V: = Range Slice9 {fmt. Printf ("slice9[%d] =%c \", I, v)//x y}*/s1: = make ([]int, 5, ten) fmt. Println ("len, Cap =", Len (S1), ",", Cap (S1))//len, Cap = 5, 10s2: = []int {1, 2, 3}changevalue2 (S2) fmt. Println ("result2:", S2)//RESULT2: [5 2 3] The result will change//mapvar map1 map[string] string//map1["a"] = "123"//If not initialized, an exception will occur Pani C:runtime error:assignment to entry in nil Mapmap1 = Make (map[string] string) map1["a"] = "123" FMT. Println ("Map1 =", map1)//map1 = map[a:123]fmt. Println (map1["B"])//(nil) b, found: = map1["B"]//found is a bool value, true = Foundif found {fmt. Println ("Found, and the value =", b)} else {fmt. Println ("not Found")}map1["B"] = "456" FMT. Println ("Map1 =", map1)//map1 = map[a:123 B:456]delete (map1, "B")//delete key to "a" for the FMT. Println ("Map1 =", map1)//map1 = map[b:456]fmt. Println ("len (MAP1) =", Len (MAP1))//len (MAP1) = 1//fmt. Println ("cap (MAP1) =", Cap (MAP1))//Invalid ARGument map1 (Type map[string]string) for capmap2: = map1map2["a"] = "789" FMT. Println ("Map1 =", map1)//map1 = map[a:789], whose value is changed because MAP2 and MAP1 are the same address space}func ChangeValue (arr [3]int) {arr[0] = 100fmt. Println ("ChangeValue:", arr)//changevalue: [5 6]}func changeValue2 (SLC []int) {slc[0] = 5fmt. Println ("ChangeValue2:", SLC)//changevalue2: [5 2 3]}
Operation Result: