I want to consider a two-dimensional real array of numbers. I got to the point where the contents of the file are stored in the data line, but I don’t know how to create a two-dimensional array of real numbers based on this: Here’s the code:

package main import ( "fmt" //"gonum.org/v1/gonum/mat" "io" "os" "strconv" ) func main() { file, err := os.Open("matrix") if err != nil{ fmt.Println(err) os.Exit(1) } defer file.Close() data := make([]byte, 64) for{ n, err := file.Read(data) if err == io.EOF{ break } fmt.Print(string(data[:n])) } } 

Content of the matrix file:

 5.482 0.358 0.237 0.409 0.416 0.580 4.953 0.467 0.028 0.464 0.319 0.372 8.935 0.520 0.979 0.043 0.459 0.319 4.778 0.126 

    1 answer 1

    It is possible so:

     r := strings.NewReader(input) s := bufio.NewScanner(r) var matrix [][]float64 for s.Scan() { records := strings.Fields(s.Text()) line := make([]float64, len(records)) matrix = append(matrix, line) for i := range records { line[i], err = strconv.ParseFloat(records[i], 64) if err != nil { panic(err) } } } if err = s.Err(); err != nil { panic(err) } 

    Playground: https://play.golang.org/p/a1ykU4udBTe .