day25.go 828 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. )
  9. func readFile(file *os.File) []int {
  10. var keys []int
  11. scanner := bufio.NewScanner(file)
  12. for scanner.Scan() {
  13. line := scanner.Text()
  14. if line == "" {
  15. break
  16. }
  17. key, err := strconv.Atoi(string(line))
  18. if err != nil {
  19. log.Fatalf("Error processing key for %s: %s", line, err)
  20. }
  21. keys = append(keys, key)
  22. }
  23. if err := scanner.Err(); err != nil {
  24. log.Fatalf("Scanner error: %s", err)
  25. }
  26. return keys
  27. }
  28. func main() {
  29. if len(os.Args) < 2 {
  30. log.Fatal("You need to specify a file!")
  31. }
  32. filePath := os.Args[1]
  33. file, err := os.Open(filePath)
  34. if err != nil {
  35. log.Fatalf("Failed to open %s!\n", filePath)
  36. }
  37. keys := readFile(file)
  38. if err := file.Close(); err != nil {
  39. log.Fatalf("Failed to close file: %s", err)
  40. }
  41. fmt.Println(keys)
  42. }