Menu
Coddy logo textTech

Declaring Arrays

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

In Go, you can declare arrays in several ways. An array's size is part of its type and must be specified when declared.

Declare an array using var with a specific size:

var scores [4]int
fmt.Println(scores)

This creates an array of 4 integers, all initialized to zero:

[0 0 0 0]

Declare and initialize an array in one line using shorthand notation:

names := [3]string{"Alice", "Bob", "Charlie"}
fmt.Println(names)

This displays:

[Alice Bob Charlie]
challenge icon

Challenge

Beginner
In this challenge, you'll practice declaring and initializing an array in Go. Your task is to declare an array of 5 integers with the values 10, 20, 30, 40, and 50, then print the array to the console.

Cheat sheet

Arrays in Go have a fixed size that must be specified when declared. The size is part of the array's type.

Declare an array with var:

var scores [4]int

Declare and initialize an array using shorthand notation:

names := [3]string{"Alice", "Bob", "Charlie"}

Arrays are initialized to zero values if not explicitly set:

[0 0 0 0]

Try it yourself

package main

import "fmt"

func main() {
    // TODO: Declare an array named 'numbers' that contains 5 integers: 10, 20, 30, 40, and 50
    
    // This will print your array
    fmt.Println("My array of numbers:", numbers)
}
quiz iconTest yourself

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

All lessons in Fundamentals