Go the language array declaration needs to specify the element type and the number of elements, with the syntax in the following format:
var variable_name [SIZE] Variable_type
The above is how one-dimensional arrays are defined. The array length must be an integer and greater than 0. For example, the following defines an array of balance lengths of type float32:
var balance [ten] float32
The following shows the initialization of an array:
var balance = [5]float32{1000.0, 2.0, 3.4, 7.0, 50.0}
initializes the array The number of elements in {} cannot be greater than the number in [].
If you ignore the number in [] does not set the array size, andtheGo language Sets the size of the array according to the number of elements:
var balance = [...] float32{1000.0, 2.0, 3.4, 7.0, 50.0}
The instance is the same as the previous instance, although the size of the array is not set.
BALANCE[4] = 50.0
with the C language as , index from 0 Start
to access an array element through an index
How multidimensional arrays are declared
var variable_name [Size1][size2] ... [Sizen] Variable_type
- Defining slices
- Declares an array of unspecified size to define a slice
var identifier []type
- Use Make () function to create a slice
var Slice1 []type = make ([]type, Len)
can also be shortened to
Slice1: = Make ([]type, Len)
- capacity can be specified for slices , which capacity as an optional parameter
- Make ([]t, length, capacity)
S: =[] int {A-i}
The Direct initialization of the slice,[] means the slice type, andthe {-I} initialization value is in order of one . its cap=len=3
S: = arr[:]
initializing slices S, is an array arr the reference
S: = Arr[startindex:endindex]
will be arr Subscript from subscript StartIndex to the endIndex-1 creates a new slice of the element under the
S: = Arr[startindex:]
default EndIndex will be represented until arr the last element
S: = Arr[:endindex]
default StartIndex is represented from the arr begins with the first element of a
S1: = S[startindex:endindex]
initializing slices by slicing s S1
S: =make ([]int,len,cap)
with built-in functions Make () initializing slices S,[]int identified as its element type is int the slices
- Slicing built-in functions
| len (Slice1) |
return slice length return length |
| cap (Slice1) |
Return tile capacity, return capacity |
| empty slice |
a slice in uninitialized money default is null, length is 0 |
| slice intercept |
See Initialize slice section |
| append (Slice1, x) |
add element element x after Slice1 (can add more) |
Copy (Slice1, Slice2) |
Copy the contents of Slice2 to Slice1 |
Example :
Package Main
Import "FMT"
Func Main () {
var numbers []int
Printslice (Numbers)
/* allow empty slices to be appended */
Numbers = append (numbers, 0)
Printslice (Numbers)
/* add an element to a slice*/
Numbers = append (numbers, 1)
Printslice (Numbers)
/* add multiple elements at the same time */
Numbers = append (numbers, 2,3,4)
Printslice (Numbers)
/* Create slice numbers1 is twice times the capacity of the previous slice */
Numbers1: = Make ([]int, Len (Numbers), (Cap (numbers)) * *)
/* copy The contents of the numbers to numbers1 * *
Copy (numbers1,numbers)
Printslice (NUMBERS1)
}
Func printslice (x []int) {
Fmt. Printf ("len=%d cap=%d slice=%v\n", Len (x), Cap (x), X)
}
go Language range keyword for for iteration group "slice (Slice) , channel (channel) key value.
Example:
Package Main
Import "FMT"
Func Main () {
// This is where we use range to ask for a slice and. Using arrays is similar to this
Nums: = []int{2, 3, 4}
Sum: = 0
For _, num: = Range Nums {
sum + = num
}
Fmt. Println ("sum:", sum)
// using range on the array will pass in the index and the value of two variables. In the above example, we don't need to use the ordinal of the element, so we omit it with the whitespace character "_" . Sometimes we really need to know its index.
For i, num: = Range Nums {
if num = = 3 {
Fmt. Println ("Index:", i)
}
}
//range It can also be used on the map 's key-value pairs.
KVS: = map[string]string{"A": "Apple", "B": "Banana"}
For k, V: = range KVS {
Fmt. Printf ("%s-%s\n", K, V)
}
//range You can also use to enumerate Unicode strings. The first argument is the index of the character, and the second is the character (theUnicode value) itself.
For I, C: = Range "Go" {
Fmt. Println (i, C)
}
}
Go language arrays, slices