From 5df40a8847586ebcb42107cdc3f4c380d59fa93c Mon Sep 17 00:00:00 2001 From: raul Date: Sun, 4 Feb 2024 09:45:14 +0100 Subject: [PATCH] Added file-reader exercise --- golangr/file-reader/go.mod | 3 +++ golangr/file-reader/main.go | 33 +++++++++++++++++++++++++++++++++ golangr/file-reader/names.txt | 4 ++++ 3 files changed, 40 insertions(+) create mode 100644 golangr/file-reader/go.mod create mode 100644 golangr/file-reader/main.go create mode 100644 golangr/file-reader/names.txt diff --git a/golangr/file-reader/go.mod b/golangr/file-reader/go.mod new file mode 100644 index 0000000..5b20b70 --- /dev/null +++ b/golangr/file-reader/go.mod @@ -0,0 +1,3 @@ +module file-reader + +go 1.21.6 diff --git a/golangr/file-reader/main.go b/golangr/file-reader/main.go new file mode 100644 index 0000000..8ce6ec5 --- /dev/null +++ b/golangr/file-reader/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "os" +) + +var names = []string{} +var err error + +func main() { + if len(os.Args) < 2 || len(os.Args) > 2 { + fmt.Println("Usage: ./file-reader names.txt") + os.Exit(1) + } + + filePath := os.Args[1] + + names, err := fileReader(filePath) + if err != nil { + fmt.Print(err) + } + fmt.Println(names) +} + +func fileReader(path string) (str string, err error) { + b, err := os.ReadFile(path) + if err != nil { + fmt.Print(err) + } + stre := string(b) + return stre, err +} diff --git a/golangr/file-reader/names.txt b/golangr/file-reader/names.txt new file mode 100644 index 0000000..532cbde --- /dev/null +++ b/golangr/file-reader/names.txt @@ -0,0 +1,4 @@ +word1 +word2 +word3 +word4