I'm new. This is my code in GO.
package main
import ( "fmt" )
func main() { var number int fmt.Print("how many candidates?: ") fmt.Scanf("%v\n", &number) fmt.Print(numero)
var name []string
var nameHorse []string
var matrix [][]int
for i := 0; i < number; i++ {
fmt.Print("name: ")
fmt.Scanf("%v\n", &name[i])
fmt.Print("name horse: ")
fmt.Scanf("%v\n", &nameHorse[i])
for j := 0; j < 3; j++ {
fmt.Print("how many minutes: ")
fmt.Scanf("%v\n", &matrix[i][0])
fmt.Print("how many segs: ")
fmt.Scanf("%v\n", &matrix[i][1])
fmt.Print("hoy many h: ")
fmt.Scanf("%v\n", &matrix[i][2])
}
}
for i := 0; i < number; i++ {
fmt.Print(name[i])
fmt.Print(nameHorse[i])
for j := 0; j < 3; j++ {
fmt.Print(matrix[i][j])
}
fmt.Print()
}
}
I want to define an array [2][4] but I want to define when the program is ON, like this:
fmt.Print("rows: ") fmt.Scanf("%v\n", &rows)
fmt.Print("columnas: ") fmt.Scanf("%v\n", &columns)
var matrix [rows][columns]int
I want to add the dimension for a project that the values will be introduced by console to the matrix.
Can somebody help me, how can I do that?
You have to use a slice if you don't know the size at compile time, and for multidimensional you'd want to use a loop:
matrix := make([][]int, rows)
for i := 0; i < rows; i++ {
matrix[i] = make([]int, columns)
}