Added file-reader exercise

This commit is contained in:
raul 2024-02-04 09:45:14 +01:00
parent 57a3fe4e69
commit 5df40a8847
3 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,3 @@
module file-reader
go 1.21.6

View File

@ -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
}

View File

@ -0,0 +1,4 @@
word1
word2
word3
word4