我在使用 GO 用文本文件中的矩阵填充二维数组时遇到问题。
我的主要问题是创建一个二维数组,因为我必须计算数组的维度,而 GO 似乎不接受数组维度中的 VAR:
nb_lines = number of line of the array
nb_col = number of columns of the array
// read matrix from file
whole_file,_ := ioutil.ReadFile("test2.txt")
// get each line of the file in tab_whole_file
tab_whole_file := strings.Split(string(whole_file), "\n")
// first line of the table
tab_first_line := strings.Split(tab_whole_file[0], "\t")
nb_col := len(tab_first_line)
nb_lines := len(tab_whole_file) - 1
// at this point I tried to build a array to contain the matrix values from the texte file
var columns [nb_lines][nb_col]float64 // does not work
columns := make([][]float64, nb_lines, nb_col) // does not work
columns := make([nb_lines][nb_col]float64) // does not work
columns := [nb_lines][nb_col]float64{} // does not work
columns := [][]float64{} // panic: runtime error: index out of range
for i := 0; i < nb_lines ; i++ { // for each line of the table from text file
line := strings.Split(tab_whole_file[0], "\t") // split one line to get each table values
for j := 1; j < len(line) ; j++ {
columns[i][j], _ = strconv.ParseFloat(line[j], 64) // assign each value to the table columns[i][j]
}
}
猛跑小猪
慕田峪4524236
回首忆惘然
相关分类