I'm trying to figure out how to change a multidimensional slice by reference.
func main() {
matrix := [][]int{
{1, 0, 0},
{1, 0, 0},
{0, 1, 1},
}
fmt.Println("Before")
printMatrix(matrix)
changeMatrixByReference(&matrix)
fmt.Println("After")
printMatrix(matrix)
}
func changeMatrixByReference(matrix *[][]int) {
//&matrix[0][0] = 3
}
func printMatrix(matrix [][]int) {
for i := 0; i < len(matrix); i++ {
for j := 0; j < len(matrix[0]); j++ {
fmt.Printf("%d", matrix[i][j])
}
fmt.Println("")
}
}
How can I change the matrix 2d slice inside the function changeMatrixByReference
? I expect when printMatrix
runs the second time matrix[0][0]
becomes 3
.