Learning Golang Language (1): Hello World
Learning Golang Language (2): Variables
Learning Golang Language (3): Type--Boolean and numeric types
Learning Golang Language (4): Type--string
Learning Golang Language (5): Type--array
Learning Golang Language (6): Type--slice
Today continue to follow the "code" to learn Golang language together. In the previous chapters we learned about the basic data types of Golang. Next we will learn Golang built-in advanced types: arrays, slices, and dictionaries. First, the array is explained.
Arrays (Array)
The go language array is a fixed-length sequence, and the array contains the same type of elements. A multidimensional array can be created simply by using its own array of elements.
The elements of the array are indexed using the operator [] and the index number starts at 0. So the first element of the array is: Array[0], and the last element is Array[len (array)-1]. Arrays can be changed by using Array[index] to the left of the assignment operator to finish setting the element content of the index position.
Arrays are created using the following syntax:
-------------------------------------------
[Length]type;
[N]type (value1,value2,..., Valuen)
[...] Type (value1,value2,..., valuen)
-------------------------------------------
In the first case, none of the elements of the array are assigned, at which point each element takes the default value: 0 value.
In the second case, an array of length n is declared, and the element is assigned a value in turn.
The symbol in the third case ... (ellipsis) operator, the Go language is automatically calculated for the length of the array.
Note :
For integers, 0 values = 0; for floating-point numbers, 0 values = 0.0, for strings, 0 values = "", and 0 values for objects, =nil.
For example:
The output is:
The example shows that if there is no explicit initialization or partial initialization when creating an array, the Go language initializes the item with no assignment to a value of 0 of the corresponding type.
Use... The symbol can automatically calculate the array length. Go provides this method of automatically calculating the length of an array to provide some convenience when debugging a program. Because we don't need to modify the length of an array if we move an item or items from the divisor group.
Multidimensional Arrays
Output:
Arrays in different and C and C++,go languages are passed by value, so it is very expensive to pass a large array. Fortunately, arrays are not often used in the go language because we can use slices (slice) instead. Next time we will explain the use of slices.
---------------------------------------------
Welcome to focus on the code of operation. Learn Golang language together.