SoFunction
Updated on 2025-03-02

A brief analysis of the use of arrays in Go language

Arrays are used to store multiple values ​​of the same type in a single variable, rather than declaring a separate variable for each value.

Declare an array

In Go, there are two ways to declare arrays:

1. UsevarKeywords:

grammar

var array_name = [length]datatype{values} // The length is defined here

or

var array_name = [...]datatype{values} // The length is implicit here

2. Use:=symbol:

grammar

array_name := [length]datatype{values} // The length is defined here

or

array_name := [...]datatype{values} // The length here is implicit

Note: The length specifies the number of elements to be stored in the array. In Go, arrays have fixed lengths. The length of the array can be defined by a number or implicit (this means that the compiler determines the length of the array based on the number of values).

Array example

This example declares two arrays with defined lengths (arr1 and arr2):

package main
import "fmt"
func main() {
  var arr1 = [3]int{1, 2, 3}
  arr2 := [5]int{4, 5, 6, 7, 8}
  (arr1)
  (arr2)
}

Output:

[1 2 3]
[4 5 6 7 8]

Example

This example declares two arrays of implicit lengths (arr1 and arr2):

package main
import "fmt"
func main() {
  var arr1 = [...]int{1, 2, 3}
  arr2 := [...]int{4, 5, 6, 7, 8}
  (arr1)
  (arr2)
}

Output:

[1 2 3]
[4 5 6 7 8]

Example

This example declares an array of strings:

package main
import "fmt"
func main() {
  var cars = [4]string{"Volvo", "BMW", "Ford", "Mazda"}
  (cars)
}

Output:

[Volvo BMW Ford Mazda]

Access array elements

You can access specific array elements by referencing index numbers.

In Go, array index starts at 0. This means [0] is the first element, [1] is the second element, and so on.

Example

This example shows how to access the first and third elements in the prices array:

package main
import "fmt"
func main() {
  prices := [3]int{10, 20, 30}
  (prices[0])
  (prices[2])
}

Output:

10
30

Change array elements

You can also change the value of a specific array element by referencing the index number.

Example

This example shows how to change the value of the third element in the prices array:

package main
import "fmt"
func main() {
  prices := [3]int{10, 20, 30}
  prices[2] = 50
  (prices)
}

Output:

[10 20 50]

Array initialization

If an array or its elements are not initialized in the code, it will be assigned the default value of its type.

Tip: The default value of int is 0, and the default value of string is "".

Example

package main
import "fmt"
func main() {
  arr1 := [5]int{}           // Not initialized  arr2 := [5]int{1, 2}       // Partial initialization  arr3 := [5]int{1, 2, 3, 4, 5} // All initialization  (arr1)
  (arr2)
  (arr3)
}

Output:

[0 0 0 0 0]
[1 2 0 0 0]
[1 2 3 4 5]

Initialize only specific elements

It is also possible to initialize only specific elements in the array.

Example

This example initializes only the second and third elements of the array:

package main
import "fmt"
func main() {
  arr1 := [5]int{1: 10, 2: 40}
  (arr1)
}

Output:

[0 10 40 0 0]

Example explanation

The array above has 5 elements.

1:10 means: assign 10 to the array index 1 (second element).

2:40 means: assign 40 to array index 2 (third element).

Find the length of the array

uselen()The function can find the length of the array:

Example

package main
import "fmt"
func main() {
  arr1 := [4]string{"Volvo", "BMW", "Ford", "Mazda"}
  arr2 := [...]int{1, 2, 3, 4, 5, 6}
  (len(arr1))
  (len(arr2))
}

Output:

4
6

This is the end of this article about a brief analysis of the use of arrays in Go. For more related go array content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!