Gaaker Go

Golang Slices of slices

If the elements of a slice are slices, then this slice constitutes a two-dimensional slice, and so on to form slices of more dimensions.

The following is a complete code example.

package main

import (
	"fmt"
	"strings"
)

func main() {

	board := [][]string{
		[]string{"-", "-", "-"},
		[]string{"-", "-", "-"},
		[]string{"-", "-", "-"},
	}

	board[1][1] = "X"

	for i := 0; i < len(board); i++ {
		fmt.Printf("%s\n", strings.Join(board[i], " "))
	}
}

Below is the program output:

- - -
- X -
- - -
< Slices of slices Slices of slices >