code.go 687 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. func readInput(file *os.File) []int {
  9. scanner := bufio.NewScanner(file)
  10. var numbers []int
  11. for scanner.Scan() {
  12. line := scanner.Text()
  13. if line == "" {
  14. continue
  15. }
  16. var current int
  17. n, err := fmt.Sscanf(line, "%d", &current)
  18. if n != 1 || err != nil {
  19. log.Fatal("Can't parse:", line, err)
  20. }
  21. numbers = append(numbers, current)
  22. }
  23. return numbers
  24. }
  25. func main() {
  26. if len(os.Args) < 2 {
  27. log.Fatal("You need to specify a file!")
  28. }
  29. filePath := os.Args[1]
  30. file, err := os.Open(filePath)
  31. if err != nil {
  32. log.Fatalf("Failed to open %s!\n", filePath)
  33. }
  34. numbers := readInput(file)
  35. fmt.Println(numbers)
  36. }