Go: How To Create a 2D Matrix Properly?
1 min readLiang

Slice or Array?
In Go, array can only be created by constant int
arr := [4]int // works
size := 4
arr := [size]int // Not work!
Thus use slice, if we want to implement a function that create a N size matrix
Implement
straight forward
- bad memory using, not contiguous matrix
func newMatrix(s int) [][]int {
m := make([][]int, s)
// Wrong!
//
// for _, row := range m {
// row = make([]int, s)
// }
//
for i, _ := range m {
m[i] = make([]int, s)
}
return m
}
method 2
- ensure to get a contiguous matrix
func newMatrix(s int) [][]int {
// allocate the whole space of the matrix
spaces := make([]int, s*s)
// create an empty matrix: [[] [] []...]
m := make([][]int, s)
// assign spaces to the matrix
i, j := 0, 0
for i < s {
m[i] = spaces[j : j+s]
i++
j += s
}
return m
}
Generic
more flexiable!
func newMatrix[T any](s int) [][]T {
// allocate the whole space of the matrix
spaces := make([]T, s*s)
// create an empty matrix: [[] [] []...]
m := make([][]T, s)
// assign spaces to the matrix
i, j := 0, 0
for i < s {
m[i] = spaces[j : j+s]
i++
j += s
}
return m
}
Usage
m := newMatrix[unit](3) // [[0 0 0] [0 0 0] [0 0 0]]