From c8c09e5f156edf1089d5a64b048abac9bca66d81 Mon Sep 17 00:00:00 2001 From: Theis Date: Wed, 6 Mar 2024 13:31:33 +0100 Subject: [PATCH] refactor: created a check_err func to shorthand error checks in code --- src_code/go_src/cocommit.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src_code/go_src/cocommit.go b/src_code/go_src/cocommit.go index b059648..200983f 100644 --- a/src_code/go_src/cocommit.go +++ b/src_code/go_src/cocommit.go @@ -37,10 +37,7 @@ func main() { authors := os.Getenv("author_file") file, err := os.Open(authors) - if err != nil { - print("File not found") - os.Exit(2) - } + check_err(err) defer file.Close() scanner := bufio.NewScanner(file) @@ -81,9 +78,7 @@ func main() { } } - if err := scanner.Err(); err != nil { - os.Exit(2) - } + check_err(scanner.Err()) // Removes the call command for the program args := os.Args[1:] @@ -209,3 +204,10 @@ func NoInput(args []string, users map[string]user) { os.Exit(1) } } + +func check_err(e error) { + if e != nil { + fmt.Println(e.Error()) + os.Exit(2) + } +}