diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2912fd9 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gat + +go 1.21.6 diff --git a/main.go b/main.go new file mode 100644 index 0000000..4eeb805 --- /dev/null +++ b/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +// https://golangr.com/file-exists + +func main() { + if len(os.Args) < 2 { + fmt.Println("Not enough arguments") + os.Exit(1) + } + + for i := 1; i < len(os.Args); i++ { + if _, err := os.Stat(os.Args[i]); err == nil { + reader(os.Args[i]) + } else { + fmt.Printf("File does not exist\n") + } + } +} + +func reader(file string) { + fil, err := os.Open(file) + if err != nil { + panic(err) + } + defer fil.Close() + + scanner := bufio.NewScanner(fil) + for scanner.Scan() { + fmt.Println(scanner.Text()) + } + + if err := scanner.Err(); err != nil { + fmt.Println(err) + } +}