2D slice in Go

Photo by Tim Cooper on Unsplash

2D slice in Go

Need to know height and width in advance

·

1 min read

package main

import "fmt"

func main() {
    height := 6
    width := 5
    foo := make([][]int, height)
    fmt.Println("foo slice: ", foo)

    for row := range foo {
        if len(foo[row]) < 1 {
            foo[row] = make([]int, width)
        }
        fmt.Println(foo[row])
    }
}