46 lines
747 B
Go
46 lines
747 B
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
func OpenFile(filename string) (*os.File, error) {
|
|
fmt.Println("Opening file",filename)
|
|
return os.Open(filename)
|
|
}
|
|
|
|
func CloseFile(filename *os.File) {
|
|
fmt.Println("Closing file")
|
|
filename.Close()
|
|
}
|
|
|
|
func GetNums(filename string) ([]int,error) {
|
|
var nums []int
|
|
file,err :=OpenFile("data.txt")
|
|
defer CloseFile(file)
|
|
if err != nil {
|
|
return nil,err
|
|
}
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
number,err := strconv.ParseInt(scanner.Text(),10,0)
|
|
if err != nil {
|
|
return nil,err
|
|
}
|
|
fmt.Println(number)
|
|
nums = append(nums, int(number))
|
|
}
|
|
if scanner.Err() != nil {
|
|
return nil,scanner.Err()
|
|
}
|
|
return nums,err
|
|
}
|
|
|
|
|
|
func main() {
|
|
GetNums("data.txt")
|
|
}
|