Go language provides an array type data structure.
An array is a sequence of numbered and fixed-length data items of the same unique type, which can be any primitive type such as an integer, a string, or a custom type.
Declare arrays
Go language array declaration needs to specify the element type and number of elements. The syntax format is as follows:
var variable_name [SIZE] variable_type
The above is the definition of one-dimensional array. For example, the following defines an array balance with a length of 10 and a type of float32:
var balance [10] float32
Initializing the array
The following demonstrates array initialization:
var balance = [5]float32{1000.0, 2.0, 3.4, 7.0, 50.0}
Initializes the number of elements in {} in the array The number cannot be greater than the number in [].
Golang variable length array:
If you ignore the number in [] and do not set the array size, the Go language will set the array size according to the number of elements. Size:
var balance = [...]float32{1000.0, 2.0, 3.4, 7.0, 50.0}
For more golang knowledge, please pay attention to the golang tutorial column.
The above is the detailed content of How to define variable length array in golang. For more information, please follow other related articles on the PHP Chinese website!