How to pass a 2 dimensional array as a function argument in Go? -
so want able pass matrix function in argument in golang. different size each time - e.g., 4x4 matrix, 3x2 matrix, etc. if try running test code below against source code error message like:
how pass 2 dimensional array function? i'm new go , come dynamic language background (python, ruby).
cannot use mat[:][:] (type [][3]int) type [][]int in argument zeroreplacematrix
source code
func replacematrix(mat [][]int, rows, cols, a, b int) { }
test code
func testreplacematrix(t *testing.t) { var mat [3][3]int //some code got := replacematrix(mat[:][:], 3, 3, 0, 1) }
the easiest way use slices. unlike arrays passed reference,not value. example:
package main import "fmt" type matrix [][]float64 func main() { onematrix := matrix{{1, 2}, {2, 3}} twomatrix := matrix{{1, 2,3}, {2, 3,4}, {5, 6,7}} print (onematrix) print (twomatrix) } func print(x matrix) { _, := range x { _, j := range { fmt.printf("%f ", j) } fmt.println() } }
Comments
Post a Comment