From 1bf9e3e27c36291367686b392205c88c66ea5fee Mon Sep 17 00:00:00 2001 From: raul Date: Tue, 6 Feb 2024 17:12:29 +0100 Subject: [PATCH] Figured out how to add file lines to array --- golangr/file-reader/main.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/golangr/file-reader/main.go b/golangr/file-reader/main.go index 8ce6ec5..cfe7286 100644 --- a/golangr/file-reader/main.go +++ b/golangr/file-reader/main.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "fmt" "os" ) @@ -16,18 +17,17 @@ func main() { filePath := os.Args[1] - names, err := fileReader(filePath) + file, err := os.Open(filePath) if err != nil { fmt.Print(err) } - fmt.Println(names) -} + defer file.Close() -func fileReader(path string) (str string, err error) { - b, err := os.ReadFile(path) - if err != nil { - fmt.Print(err) + scanner := bufio.NewScanner(file) + for scanner.Scan() { + names = append(names, scanner.Text()) } - stre := string(b) - return stre, err + + fmt.Println(names) + }