Package the main
Import (
"Datafile
""FMT"
"Log"
)
Func main () {
Numbers, err:=datafile. GetFloats (" data. TXT ")
if err ! Nil={
The Fatal (err)
}
Var sum float64=0
For _, number:=range Numbers {
The sum +=number
}
SampleCount:=float64 (len (Numbers))
FMT. Printf (" business: % 0.2 f \ n ", the sum/sampleCount)
}
This is the calling code, in the work area under SRC/datafile
Package datafile
Import (
"Bufio
""OS"
"Strconv"
)
Func GetFloats fileName (string) ([] float64, error) {
Var Numbers [] float64
The file, err:=OS. Open (fileName)
if err ! Nil={
Return Numbers, err
}
Scanner:=bufio NewScanner (file)
For scanner. The Scan () {
Number, err:=strconv ParseFloat (scanner. The Text (), 64)
if err ! Nil={
Return Numbers, err
}
Numbers=append (Numbers, number)
}
Err=file. The Close ()
if err ! Nil={
Return Numbers, err
}
If the scanner. Err ()! Nil={
Return Numbers, scanner. Err ()
}
Return Numbers, nil
}
Then, run the first code of time vs code to the error
I want to ask is this error because I couldn't find the data. TXT