code.go 550 B

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