This is a creation in Article, where the information may have evolved or changed.
Array
The array declaration--var arr [10]int,array is defined by [n]<type>. n = array size, type
Declare and initialize--arr : = [3]int{1,2,3} or do not write size var arr : = [...] int{1,2,3} or var arr = [3]int{1,2,3}, if no write size can be represented by three points, go will automatically recognize that the Var arr[3]int default is initialized to 0.
PS: Remember: the difference between = and =, if it is: = Do not need Var, if it is = need var
Two-dimensional array initialization A: = [2][2]int{[2]int{1,2}, [2]int{3,4}},a: = [2][2]int{ [...] int{1,2}, [...] int{3,4} },a: = [2][2]int{{}, {3,4}} The default type is int
Arrays are also value types: assigning an array to another array will copy all the elements. Especially when passing an array to a function, it gets a copy of the array, not the pointer to the array.
Slice
The slice is close to the array, but can increase the length when new elements are added. Slice always points to an array at the bottom. Slice is a pointer to an array, which is different from the array;Slice is a reference type , which means that when you assign a slice to another variable, two references point to the same array. For example, if a function requires a slice parameter, modifications to the slice element in it will also be reflected in the function caller, similar to passing the underlying array pointer.
Slice always appears in pairs with a fixed-length array. It affects the capacity and length of the slice
Arr: = [ten]int{}slice:= arr[0:5]slice1:= append (slice,4,5,6) fmt. Printf ("Slice1 len:%d, Slice1 cap:%d\n", Len (Slice1), Cap (Slice1))
Fmt. Printf ("Slice len:%d, slice cap:%d\n", Len (slice), cap (slice))FMT. Printf ("arr[0]=%d,arr[1]=%d,arr[2]=%d,arr[3]=%d,arr[6]=%d\n", Arr[0],arr[1],arr[2],arr[3],arr[6])
ARR[6] = 5, indicating that both slice and Slice1 point to arr. Changing slice and Slice1 will change arr.
The function append appends a value of 0 or other X to slice S, and returns the new appended, slice with the same type as S. If s does not have enough capacity to store the appended value, append allocates a large enough, new slice to hold the original slice element and the appended value. Therefore, the returned slice may point to a different underlying array.
That is, when slice points to an arr, if you increase the capacity of slice by append, if the number of added values does not exceed the length of arr, slice still points to arr, and if the length of arr is exceeded, a new slice is allocated to hold the original slice.
Make
You can create a SLICE--SL with a length of n by making: = Make ([]int,6)
Map
1 Package Main2 3Import "FMT"4 5 Func Main () {6Monthdays: = map[string]int{7"Jan": "Feb": +, "Mar": 31, 8"APR": +, "may": +, "June": 30, 9"Jul": +, "SEP": 30, Ten"OCT": +, "Nov": +, "Dec": 31,//<--commas are required One } A -Fmt. Printf ("%d\n", monthdays["Dec"]) -Year: = 0 theFor month,days: =Range Monthdays { -Fmt. Printf ("Month:%s\n", month) -Year + = Days - } +Fmt. Printf ("Numbers of Days in a year:%d\n", year) -var valueint + var ok bool A Value , OK = monthdays["Jan"]//value will fetch the value, and OK determines if the value is actually taken at ifOK = =true { -Fmt. Printf ("Value%d exists in map\n", value) - } -}