Home > Backend Development > Golang > How to define variable length array in golang

How to define variable length array in golang

Release: 2019-12-13 15:24:55
Original
11732 people have browsed it

How to define variable length array in golang

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
Copy after login

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
Copy after login

Initializing the array

The following demonstrates array initialization:

var balance = [5]float32{1000.0, 2.0, 3.4, 7.0, 50.0}
Copy after login

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}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template