Slice of Slices

Slices can contain other slices, which is how you create multi-dimensional structures (like matrices) in Go.

Example: A 2D Grid

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

fmt.Println(rows[1][0]) // 4

Dynamic Creation

You can use make to initialize the outer slice and then loop to initialize each inner slice.

matrix := make([][]int, 5)
for i := range matrix {
    matrix[i] = make([]int, 10)
}

Flexibility

Because these are slices, each “row” can technically have a different length, although they are most commonly used for uniform grids.