Ad space (banner)
๐ŸนGo Lessons
Lesson 24 / 68

Two-Dimensional Slices (Tabular Data)

In this lesson you will learn how to use two-dimensional slices in Go so you can work with data laid out as a table. This is for anyone searching for "Go 2D slice how to use".

Putting slices inside a slice gives you a grid with rows and columns. This is called a two-dimensional slice, and you access it as grid[row][column].

The sample code declares two rows of three columns and reads a value with grid[1][2], then prints each row with for _, row := range grid. Notice how printing a slice directly shows it in the [1 2 3] form.

A common stumbling block for beginners is the order of the indexes. grid[1][2] means "row 2, column 3", and mixing up which is which gives you the wrong value.

In real-world development this idea applies whenever you want to handle grid-shaped data efficiently, from a game board with coordinates to large two-dimensional data in image processing.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)