Arrays

Question Click to View Answer

What does the following code print to the console?

var sauces [3]string
sauces[0] = "ketchup"
sauces[1] = "bbq"
sauces[2] = "mayo"
fmt.Println(sauces)

[ketchup bbq mayo] is printed.

sauces is declared as an array that holds three string elements.

The string ketchup is added in the first position of the sauces array.

The string bbq is added in the second position of the sauces array.

The string mayo is added in the third position of the sauces array.

The sauces array is then printed to the console with the fmt.Println function.

What does the following code print to the console?

numbers := [3]int{20, 22, 29}
fmt.Println(numbers)

[20 22 29] is printed to the console.

This example uses Go's shorthand syntax to create an array.

Go arrays have a fixed length and the numbers array in this example can only hold three items.

What does the following code print to the console?

primes := [5]int{1, 2, 3}
fmt.Println(primes)

[1 2 3 0 0] is printed to the console.

The primes array is declared to contain five integer values. Only three integer values are set explicitly, so the final two elements use the "zero value" for integers. The zero value for integers is 0.

What does the following code print to the console?

cheeses := [5]string{"parm", "blue", "gouda"}
fmt.Println(cheeses[2])

gouda is printed to the console.

cheeses[2] fetches the third item from the cheeses array.

What does the following code print to the console?

countries := [2]string{"peru", "argentina"}
countries[1] = "uruguay"
fmt.Println(countries)

[peru uruguay] is printed.

The countries array is initially set to contain peru and argentina. Then the second element of the array is replaced with uruguay.

What does the following code print to the console?

colors := [2]string{"red", "yellow", "blue"}
fmt.Println(colors)

This code returns the following error: array index 2 out of bounds [0:2].

The colors array is declared to only contain two strings. Go will throw an error if you try to add a third string to an array that's declared to only contain two strings.

What does the following code print to the console?

pastas := [4]string{"carbonara", "pesto"}
fmt.Println(len(pastas))

4 is printed.

The pastas array contains two values that are set and two string "zero values". The zero value for a string is the empty string. The len() function returns the number of items in an array.

What does the following code print to the console?

soups := [4]string{
    "pho",
    "chicken noodle",
    "sancocho",
}
fmt.Println(soups[2])

sancocho is printed.

soups[2] returns the third item in the soups array.

What does the following code print to the console?

balkans := [2]string{
    "croatia",
    "serbia"
}
fmt.Println(balkans)

The code returns the following error: syntax error: unexpected newline, expecting comma or }.

When the multiline array syntax is used, each element must be followed by a comma.

This code should be refactored as follows:

balkans := [2]string{
    "croatia",
    "serbia",
}
fmt.Println(balkans)