From 2577b92b2f6025802531d7cd1a21b64aa890a81a Mon Sep 17 00:00:00 2001 From: raul Date: Tue, 27 Feb 2024 07:04:25 +0000 Subject: [PATCH] Added background checker with goroutines --- golangr/goroutines/main.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/golangr/goroutines/main.go b/golangr/goroutines/main.go index db5dc93..dae4a53 100644 --- a/golangr/goroutines/main.go +++ b/golangr/goroutines/main.go @@ -2,17 +2,39 @@ package main import ( "fmt" + "os" "time" ) -func printText(s string) { - for i := 0; i < 10; i++ { - time.Sleep(time.Millisecond * 50) - fmt.Println(s) +var check bool = false +var choice string + +// func printText(s string) { +// for i := 0; i < 10; i++ { +// time.Sleep(time.Millisecond * 50) +// fmt.Println(s) +// } +// } + +func bgChecker() { + for { + if check == true { + fmt.Println("WOOOOOOOOOOOO") + os.Exit(0) + } + time.Sleep(time.Second) } } func main() { - go printText("FOO") - printText("BAR") + go bgChecker() + for { + fmt.Printf("Would you like to set the check to true? [y/n] ") + fmt.Scanln(&choice) + if choice == "y" { + check = true + } else { + fmt.Println("Too bad") + } + } }