sdf
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.24.6
|
||||
@@ -0,0 +1,45 @@
|
||||
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")
|
||||
}
|
||||
Reference in New Issue
Block a user