Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Go Language Arrays

Go Language Arrays

The Go language provides the array data structure.

An array is a numbered sequence of fixed-length data items all of the same unique type, which can be any primitive type such as integer, string, or a custom type.

Compared with declaring number0, number1, ..., number99 variables, using the array form numbers[0], numbers[1] ..., numbers[99] is more convenient and easier to extend.

Array elements can be read (or modified) by index (position); indexing starts at 0, the first element has index 0, the second has index 1, and so on.


Declaring Arrays

Declaring an array in Go requires specifying the element type and the number of elements, with the following syntax:

var arrayName [size]dataType

Where

arrayName

is the name of the array,

size

is the size of the array,

dataType

is the data type of the elements in the array.

The following defines an array balance with length 10 and type float32:

var balance [10]float32


Initializing Arrays

The following demonstrates array initialization:

The following example declares an integer array named numbers with a size of 5; at declaration, each element is initialized by default according to its data type, and for integer types the initial value is 0.

var numbers [5]int

You can also use an initialization list to initialize the elements of an array:

var numbers = [5]int{1, 2, 3, 4, 5}

The above code declares an integer array of size 5 and initializes its elements to 1, 2, 3, 4, and 5 respectively.

In addition, you can use the := short declaration syntax to declare and initialize an array:

numbers := [5]int{1, 2, 3, 4, 5}

The above code creates an integer array named numbers, sets its size to 5, and initializes the element values.

**Note:** In Go, the size of an array is part of its type, so arrays of different sizes are incompatible; that is, [5]int and [10]int are different types.

The following defines an array balance with length 5 and type float32, and initializes the array elements:

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

We can also quickly initialize an array at declaration time using a literal:

balance := [5]float32{1000.0, 2.0, 3.4, 7.0, 50.0}

If the array length is uncertain, you can use ... in place of the length, and the compiler will infer the length from the number of elements:



var balance = [...]float32{1000.0, 2.0, 3.4, 7.0, 50.0}

或

balance := [...]float32{1000.0, 2.0, 3.4, 7.0, 50.0}

If the array length is set, we can also initialize elements by specifying an index:

//  将索引为 1 和 3 的元素初始化

balance := [5]float32{1:2.0,3:7.0}

The number of elements in {} of the initialization array cannot exceed the number in [].

If you omit the number in [] and do not set the array size, Go will set the array size based on the number of elements:

 balance[4] = 50.0

The above example reads the fifth element. Array elements can be read (or modified) by index (position); indexing starts at 0, the first element has index 0, the second has index 1, and so on.


Accessing Array Elements

Array elements can be read by index (position). The format is the array name followed by brackets, with the index value inside the brackets. For example:



var salary float32 = balance[9]

The above example reads the value of the 10th element of the array balance.

The following demonstrates a complete example of array operations (declaration, assignment, access):

Example 1

package

main

import

"fmt"

func

main

()

{

   

var

n

[

10

]

int

/* n is an array of length 10 */

   

var

i

,

j

int

   

/* Initialize elements for array n */

       

   

for

i

=

0

;

i

<

10

;

i

++

{

      n

[

i

]

=

i

100

/* Set the element to i + 100 */

   

}

   

/* Output the value of each array element */

   

for

j

=

0

;

j <

10

;

j

++

{

      fmt

.

Printf

(

"Element[%d] = %d\n"

,

j

,

n

[

j

]

)

   

}

}

The above example produces the following result:

Element[0] = 100

Element[1] = 101

Element[2] = 102

Element[3] = 103

Element[4] = 104

Element[5] = 105

Element[6] = 106

Element[7] = 107

Element[8] = 108

Element[9] = 109

Example 2

package

main

import

"fmt"

func

main

()

{

   

var

i

,

j

,

k

int

   

// Quickly initialize the array at the same time as declaring it

   balance

:=

[

5

]

float32

{

1000

.

0

,

2

.

0

,

3

.

4

,

7

.

0

,

50

.

0

}

   

/* Output array elements */

       

...

   

for

i

=

0

;

i

<

5

;

i

++

{

      fmt

.

Printf

(

"balance[%d] = %f\n"

,

i

,

balance

[

i

]

)

   

}

   

   balance2

:=

[

...

]

float32

{

1000

.

0

,

2

.

0

,

3

.

4

,

7

.

0

,

50

.

0

}

   

/* Output the value of each array element */

   

for

j

=

0

;

j <

5

;

j

++

{

      fmt

.

Printf

(

"balance2[%d] = %f\n"

,

j

,

balance2

[

j

]

)

   

}

   

// Initialize the elements with indices 1 and 3

   balance3

:=

[

5

]

float32

{

1

:

2

.

0

,

3

:

7

.

0

}

 

   

for

k

=

0

;

k <

5

;

k

++

{

      fmt

.

Printf

(

"balance3[%d] = %f\n"

,

k

,

balance3

[

k

]

)

   

}

}

The above example produces the following result:

balance[0] = 1000.000000

balance[1] = 2.000000

balance[2] = 3.400000

balance[3] = 7.000000

balance[4] = 50.000000

balance2[0] = 1000.000000

balance2[1] = 2.000000

balance2[2] = 3.400000

balance2[3] = 7.000000

balance2[4] = 50.000000

balance3[0] = 0.000000

balance3[1] = 2.000000

balance3[2] = 0.000000

balance3[3] = 7.000000

balance3[4] = 0.000000


More content

Arrays are very important in Go; below we will introduce more about arrays:

Content Description
Multidimensional Arrays Go supports multidimensional arrays; the simplest multidimensional array is a two-dimensional array
Passing Arrays to Functions You can pass an array argument to a function