From fa015f4837bc9092403c8a0052d01228c7e0ce28 Mon Sep 17 00:00:00 2001 From: raul Date: Fri, 9 Feb 2024 09:35:46 +0000 Subject: [PATCH] Added guessing string --- golangr/file-reader/main.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/golangr/file-reader/main.go b/golangr/file-reader/main.go index 0f9803e..5ea2c8f 100644 --- a/golangr/file-reader/main.go +++ b/golangr/file-reader/main.go @@ -5,10 +5,12 @@ import ( "fmt" "math/rand" "os" + //"strconv" ) var names = []string{} var err error +var guess string func main() { if len(os.Args) < 2 || len(os.Args) > 2 { @@ -20,7 +22,21 @@ func main() { randWord := getWord(filePath) fmt.Printf("The chosen name is %v\n", randWord) + fmt.Scan(&guess) + if guess == randWord { + fmt.Printf("You win!\n") + } else { + fmt.Printf("You lose!\n") + } + +} + +func catchErr(err error) { + if err != nil { + fmt.Println(err) + os.Exit(2) + } } func getWord(path string) (word string) { @@ -28,6 +44,7 @@ func getWord(path string) (word string) { file, err := os.Open(path) if err != nil { fmt.Println(err) + os.Exit(2) } defer file.Close()