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:
3Challenge
BeginnerIn 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: 3Try 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
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions10Composite Types: Arrays
Introduction to ArraysDeclaring ArraysInitializing ArraysAccessing Array ElementsArray Length with `len`Iterating Over ArraysMulti-dimensional Arrays2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions