bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Go/Go Tutorial
Go•Go Tutorial

Go Arrays

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Go Arrays?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ = [ length
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Array Initialization
Change Elements of an Array
Access Elements of an Array

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

Declare an Array

In Go, there are two ways to declare an array:

Syntax

var
array_name = [ length
]
datatype {
 values
} // here length is defined
or
var
array_name = [...]
datatype {
 values
}
// here length is inferred

Syntax

array_name
:= [ length
]
datatype {
 values
} // here length
is defined
or
array_name
:= [...]
datatype {
 values
}
// here length is inferred

Note

The length specifies the number of elements to store in the array. In Go, arrays have a fixed length. The length of the array is either defined by a number or is inferred (means that the compiler decides the length of the array, based on the number of values ).

Access Elements of an Array

You can access a specific array element by referring to the index number.

In Go, array indexes start at 0. That means that [0] is the first element, [1] is the second element, etc.

Example

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

Change Elements of an Array

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

Example

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

Array Initialization

If an array or one of its elements has not been initialized in the code, it is assigned the default value of its type.

Tip

The default value for int is 0, and the default value for string is "".

Example

package main
import ("fmt")
func main() {
  arr1 := [5]int{} //not initialized
  arr2 := [5]int{1,2} //partially initialized
  arr3 := [5]int{1,2,3,4,5} //fully initialized
  fmt.Println(arr1)
  fmt.Println(arr2)
  fmt.Println(arr3)
}

Initialize Only Specific Elements

It is possible to initialize only specific elements in an array.

Example

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

The array above has 5 elements.

  • 1:10 means: assign 10 to array index 1 (second element).
  • 2:40 means: assign 40 to array index 2 (third element).

Find the Length of an Array

The len() function is used to find the length of an array:

Example

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

Previous

Go String Data Type

Next

Go Slices