スライスのスライス

スライスは、他のスライスを含む任意の型を含むことができます。

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. // Create a tic-tac-toe board.
  8. board := [][]string{
  9. []string{"_", "_", "_"},
  10. []string{"_", "_", "_"},
  11. []string{"_", "_", "_"},
  12. }
  13. // The players take turns.
  14. board[0][0] = "X"
  15. board[2][2] = "O"
  16. board[1][2] = "X"
  17. board[1][0] = "O"
  18. board[0][2] = "X"
  19. for i := 0; i < len(board); i++ {
  20. fmt.Printf("%s\n", strings.Join(board[i], " "))
  21. }
  22. }