Menu
Coddy logo textTech

Multi-dimensional Arrays

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

Multi-dimensional arrays are arrays that contain other arrays as elements. They're useful for representing grids or tables.

Create a 2×3 array (2 rows, 3 columns) to represent a grid of numbers:

grid := [2][3]int{
    {1, 2, 3},
    {4, 5, 6},
}

Access elements using two indices - first for row, then for column:

element := grid[0][2]
fmt.Println(element)

This displays the element at row 0, column 2:

3
challenge icon

Challenge

Beginner

In this challenge, you'll work with a multi-dimensional array that represents a small grid of numbers. Your task is to access and print a specific element from this 2D array.

The grid represents a simple 3x3 game board with numbers:

1 2 3
4 5 6
7 8 9

Your job is to print the value at row 1, column 2 (which should be the number 6).

Cheat sheet

Multi-dimensional arrays contain other arrays as elements, useful for representing grids or tables.

Create a 2D array:

grid := [2][3]int{
    {1, 2, 3},
    {4, 5, 6},
}

Access elements using two indices - first for row, then for column:

element := grid[0][2]  // row 0, column 2
fmt.Println(element)   // prints: 3

Try it yourself

package main

import "fmt"

func main() {
	// This is a 3x3 grid represented as a 2D array
	grid := [][]int{
		{1, 2, 3},
		{4, 5, 6},
		{7, 8, 9},
	}
	
	// TODO: Access and print the value at row 1, column 2 (should be 6)
	// Remember: array indices start at 0, so row 1 is the second row
	
	// Write your code below this line
	
}
quiz iconTest yourself

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

All lessons in Fundamentals