33 lines
593 B
Go
33 lines
593 B
Go
|
package datafile
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
// GetFloats reads a float64 from each line of a file.
|
||
|
func GetFloats(fileName string) ([]float64, error) {
|
||
|
var numbers []float64
|
||
|
file, err := os.Open(fileName)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
scanner := bufio.NewScanner(file)
|
||
|
for scanner.Scan() {
|
||
|
number, err := strconv.ParseFloat(scanner.Text(), 64)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
numbers = append(numbers, number)
|
||
|
}
|
||
|
err = file.Close()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if scanner.Err() != nil {
|
||
|
return numbers, scanner.Err()
|
||
|
}
|
||
|
return numbers, nil
|
||
|
}
|