Added guessing string

This commit is contained in:
raul 2024-02-09 09:35:46 +00:00
parent de2229ada1
commit fa015f4837
1 changed files with 17 additions and 0 deletions

View File

@ -5,10 +5,12 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
//"strconv"
) )
var names = []string{} var names = []string{}
var err error var err error
var guess string
func main() { func main() {
if len(os.Args) < 2 || len(os.Args) > 2 { if len(os.Args) < 2 || len(os.Args) > 2 {
@ -20,7 +22,21 @@ func main() {
randWord := getWord(filePath) randWord := getWord(filePath)
fmt.Printf("The chosen name is %v\n", randWord) 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) { func getWord(path string) (word string) {
@ -28,6 +44,7 @@ func getWord(path string) (word string) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(2)
} }
defer file.Close() defer file.Close()