This is a creation in Article, where the information may have evolved or changed.
1. Define an array
The array length is also part of the type, such as an int array of length 3 and an int array of length 5, not the same type.
Package Mainimport ("StrConv" //converting int to string in the go language is tricky, usually using StrConv. Itoa (i))//format output An array, because arrays of different lengths belong to different types, where only slices are used as parameters, and the array is converted into slices when called .Func Print_array (arr []int) { varStrstringStr="[" forK, V: =Range Arr {ifK = = Len (arr)-1{str= str +StrConv. Itoa (v)}Else{str= str + StrConv. Itoa (v) +","}} str+="]"println (str)}func main () {//1, specify the array length: the number of elements initialized should be less than or equal to the array length, or compile an error, if the initialized element length is less than the number of arrays, then uninitialized part of the system will automatically fill the default 0 valueArray1: = [3]int{}//equivalent to [0,0,0]Print_array (array1[:]) Array2:= [3]int{1,2}//equivalent to [1,2,0]Print_array (array2[:])//arr: = [3]int{1, 2, 3, 4}//initialization error if the number of elements is greater than the array lengthArray3: = [3]int{1:2,2:3}//equivalent to [0,2,3], like a struct, go also supports the initialization of the specified key, where the key represents the index, the uninitialized index value is the default 0 valuePrint_array (array3[:])//2, does not specify the array length: the length of the array is determined by the initialized elementArray4: = [...]int{1,2,3}//equivalent to [ +/-]Print_array (array4[:]) Array5:= [...]int{1:2,2:3}//equivalent to [0,2,3]Print_array (array5[:])}
2. Array manipulation
You can access and modify operations by using array names and arrays for the. Range can be traversed. Because the length of the array is fixed, there is no capacity concept, so the concept and use are relatively simple.
For ... range can be used for ARRAY\SLICE\MAP traversal, and if a string is traversed, the default value is the ASCII code of the corresponding character, which needs to be noted.
// The following will output the 97,98,99,100, " ABCD " for _, V: = range S { ",") }
3, the characteristics of the array
An array in the Go language is a value type that copies a copy of an array when assigned to another array variable or passed as a method parameter.
Note that unlike the C language, the name of an array does not represent a pointer to the first element of the array, the array storage structure of the two languages is different, the go language array storage structure is, the header stores the array length, and then the actual data is followed.