Home »
Golang
Go Multi-Dimensional Slices
Last Updated : May 17, 2025
In Go, a multi-dimensional slice is a slice of slices. With the help of these, you can represent matrices, grids, or tables.
You can create and iterate over multi-dimensional slices using nested for
loops or for range
loops.
Create a Multi-Dimensional Slice
You can declare a multi-dimensional slice where each element is itself a slice. Here's how you can define one:
distances := [][]int{
{0, 1400, 1500}, // Delhi
{1400, 0, 1000}, // Mumbai
{1500, 1000, 0}, // Kolkata
}
Example
In this example, we are creating a 2D slice that holds distances (in km) between Indian cities:
package main
import "fmt"
func main() {
distances := [][]int{
{0, 1400, 1500}, // Delhi
{1400, 0, 1000}, // Mumbai
{1500, 1000, 0}, // Kolkata
}
fmt.Println("Distance matrix (in km):")
fmt.Println(distances)
}
When executed, this program outputs:
Distance matrix (in km):
[[0 1400 1500] [1400 0 1000] [1500 1000 0]]
Example
In this example, we are creating a 2D slice that holds student names and their ages:
package main
import "fmt"
func main() {
students := [][]string{
{"Aarav", "20"},
{"Diya", "21"},
{"Kabir", "19"},
{"Isha", "22"},
}
fmt.Println("Student details (Name and Age):")
for _, student := range students {
fmt.Printf("Name: %s, Age: %s\n", student[0], student[1])
}
}
When executed, this program outputs:
Student details (Name and Age):
Name: Aarav, Age: 20
Name: Diya, Age: 21
Name: Kabir, Age: 19
Name: Isha, Age: 22
Iterate Over a Multi-Dimensional Slice
You can iterate over a 2D slice using nested for
loops.
Example
In this example, we are printing each row and column value of the distance matrix:
package main
import "fmt"
func main() {
distances := [][]int{
{0, 1400, 1500},
{1400, 0, 1000},
{1500, 1000, 0},
}
for i := 0; i < len(distances); i++ {
for j := 0; j < len(distances[i]); j++ {
fmt.Printf("Distance[%d][%d] = %d km\n", i, j, distances[i][j])
}
}
}
When executed, this program outputs:
Distance[0][0] = 0 km
Distance[0][1] = 1400 km
Distance[0][2] = 1500 km
Distance[1][0] = 1400 km
Distance[1][1] = 0 km
Distance[1][2] = 1000 km
Distance[2][0] = 1500 km
Distance[2][1] = 1000 km
Distance[2][2] = 0 km
Iterate Using Nested for range Loops
The for range
loop can also be used in nested form to simplify iteration.
Example
In this example, we are using for range
to print the distance matrix:
package main
import "fmt"
func main() {
distances := [][]int{
{0, 1400, 1500},
{1400, 0, 1000},
{1500, 1000, 0},
}
for i, row := range distances {
for j, value := range row {
fmt.Printf("Distance[%d][%d] = %d km\n", i, j, value)
}
}
}
When executed, this program outputs:
Distance[0][0] = 0 km
Distance[0][1] = 1400 km
Distance[0][2] = 1500 km
Distance[1][0] = 1400 km
Distance[1][1] = 0 km
Distance[1][2] = 1000 km
Distance[2][0] = 1500 km
Distance[2][1] = 1000 km
Distance[2][2] = 0 km
Removing from Two-Dimensional Slice
You can remove an element from a 2D slice by using slicing and the append()
function.
Example
In this example, we are removing a student entry from a 2D slice that holds student names and their ages:
package main
import "fmt"
func main() {
students := [][]string{
{"Aarav", "20"},
{"Diya", "21"},
{"Kabir", "19"},
{"Isha", "22"},
}
fmt.Println("Original student list:")
for _, student := range students {
fmt.Printf("Name: %s, Age: %s\n", student[0], student[1])
}
// Remove the student at index 1 (Diya)
indexToRemove := 1
students = append(students[:indexToRemove], students[indexToRemove+1:]...)
fmt.Println("\nUpdated student list after removing index 1:")
for _, student := range students {
fmt.Printf("Name: %s, Age: %s\n", student[0], student[1])
}
}
When executed, this program outputs:
Original student list:
Name: Aarav, Age: 20
Name: Diya, Age: 21
Name: Kabir, Age: 19
Name: Isha, Age: 22
Updated student list after removing index 1:
Name: Aarav, Age: 20
Name: Kabir, Age: 19
Name: Isha, Age: 22
Exercise
Select the correct option to complete each statement about multi-dimensional slices in Go.
- A multi-dimensional slice is a slice of ___.
- To iterate over a 2D slice, we use ___.
- What is the output of the following code snippet?
In this example, we are printing the value at row 1 and column 2 of the matrix:
matrix := [][]int{
{10, 20, 30},
{40, 50, 60},
{70, 80, 90},
}
fmt.Println(matrix[1][2])
Advertisement
Advertisement