Go logo

Arrays in Go Language

Go language provides a data structure known as the array, which may save a fixed-size sequential collection of elements of the exact same type.

A few important points about the Go Arrays:

  • An array is a fixed-length sequence that’s used to keep a similar type of elements in the memory.
  • it’s often more helpful to consider an array as a set of variables of the exact same type.
  • The type could be anything such as integers, string, or self-defined type.
  • Rather than declaring different variables, for example, State0, State1,…, and State40, you declare one array variable including numbers and use State[0], State[1], and…, State[40] to represent individual array.
  • A particular element in an array is accessed by an index.
  • The items in the array can be accessed via their index that starts at 0 index.
  • The size or length of the array is determined by the number of items. It’s fixed and has to be declared in the declaration of an array variable.
Further Reading:  What is a Range in Go Language?
How to declare an array in Go Lang?

This is how an array is created in Go:

var name_of_array[length]Type

Just like simple variables, the var keyword is used to declare an array.

For example:

var name_arr [5]int

You also declare an array with its items:

var name_of_array[length]Typle{item1, item2, item3, …itemN}

An example of declaring and accessing array items

In this example, we have created an array of four elements. The type is int and values are given at the time of array declaration.

Then we used a for loop for accessing and displaying its elements as follows:

package main




import "fmt"




func main() {




arr_num:= [4]int{5, 10, 15, 20}




fmt.Println("Items in the array:")




for i:= 0; i < 4; i++{

fmt.Println(arr_num[i])

}




}

 

Output:

Items in the array:

5

10

15

20

Note: The number of values between braces {} can’t be larger than the number of elements that we declare for the array between square brackets [ ].

Getting the length of an array by len

You may use the default len for getting the length of an array. The example below shows using the len:

package main




import "fmt"




func main() {




    var arr_flt [5]float32




    fmt.Println("The length of array:", len(arr_flt))




}

 

Further Reading:  What is a pointer in Go Language?

Output:

The length of array: 5

You may use it in for loop as shown below:

package main




import "fmt"




func main() {




arr_str:= [3]string{"Hello", "Go", "Lang"}




fmt.Println("Items in the array:")




for i:= 0; i < len(arr_str); i++{

fmt.Println(arr_str[i])

}




}

 

The output:

Items in the array:

Hello

Go

Lang

Multidimensional arrays in Go Lang

Multi-Dimensional arrays would be the arrays of arrays of the identical type. A multidimensional array is merely a listing of one-dimensional arrays.

Go programming language enables creating multidimensional arrays. Here’s the general form of a multidimensional array declaration:

Name_of_array[Length1][Length2]..[LengthN]Type

For example:

Arr = [3][5] int

How to initialize a two-dimensional array

This is how a two-dimensional array can be initialized:

numArr = [2][3]int{   

   {10, 15, 20} ,

   {5, 10, 15} , 

}

 

The above is two rows and three columns two-dimensional array.

A running example of creating and accessing two-dimensional array

In the program below, we created a two-dimensional array that contains 2 rows and three columns. Then we accessed its items as shown below:

package main 

import "fmt" 

func main() { 




   var a = [2][3]int{{10,20,30},

        {40,50,60}} 

   var x, y int 




   for  x = 0; x < 2; x++ { 

      for y = 0; y < 3; y++ { 

         fmt.Print(a[x][y], "\t" ) 

      } 

      fmt.Println() 

   } 

}

 

Further Reading:  What is for loop in Go?

The output:

10        20        30

40        50        60