浏览代码

Able to read input

Piotr Czajkowski 2 年之前
父节点
当前提交
e88f353be4
共有 1 个文件被更改,包括 40 次插入0 次删除
  1. 40 0
      08/code.go

+ 40 - 0
08/code.go

@@ -0,0 +1,40 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+)
+
+func readInput(file *os.File) [][]byte {
+	scanner := bufio.NewScanner(file)
+	var trees [][]byte
+
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			continue
+		}
+
+		trees = append(trees, []byte(line))
+	}
+
+	return trees
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("You need to specify a file!")
+	}
+
+	filePath := os.Args[1]
+	file, err := os.Open(filePath)
+	if err != nil {
+		log.Fatalf("Failed to open %s!\n", filePath)
+
+	}
+
+	trees := readInput(file)
+	fmt.Println(trees)
+}