Menu
Coddy logo textTech

Initializing Arrays

Part of the Fundamentals section of Coddy's GO journey — lesson 69 of 109.

Initializing Arrays means setting initial values when creating an array.

Initialize an array with specific values using curly braces:

scores := [4]int{95, 87, 78, 92}
fmt.Println(scores)

This displays:

[95 87 78 92]

Use the ellipsis (...) to let Go count the elements for you:

colors := [...]string{"Red", "Green", "Blue"}
fmt.Println(colors)
fmt.Println("Array length:", len(colors))

This outputs:

[Red Green Blue]
Array length: 3
challenge icon

Challenge

Beginner

In this challenge, you'll practice initializing arrays in Go. Your task is to create an array of 5 integers with the specific values provided, then print the array.

The code already has the necessary imports and a main function. You just need to initialize the array with the given numbers.

Cheat sheet

Initialize an array with specific values using curly braces:

scores := [4]int{95, 87, 78, 92}

Use the ellipsis (...) to let Go count the elements automatically:

colors := [...]string{"Red", "Green", "Blue"}

Get array length with len():

fmt.Println("Array length:", len(colors))

Try it yourself

package main

import "fmt"

func main() {
    // TODO: Initialize an array called 'favoriteNumbers' with these 5 integers: 7, 42, 8, 13, 99
    // You can use either syntax:
    // favoriteNumbers := [5]int{7, 42, 8, 13, 99}
    // OR
    // var favoriteNumbers [5]int = [5]int{7, 42, 8, 13, 99}
    
    // This will print your array
    fmt.Printf("My favorite numbers are: %v\n", favoriteNumbers)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals