From 2a0ce3f89df985b850a73def691cefb44e40261d Mon Sep 17 00:00:00 2001 From: raul Date: Wed, 20 Mar 2024 20:59:48 +0100 Subject: [PATCH] Minor tweaks --- golangr/goroutines/2.go | 24 +++++++++++++++++------- golangr/goroutines/main.go | 16 +++++++++++----- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/golangr/goroutines/2.go b/golangr/goroutines/2.go index 212bb5a..3070720 100644 --- a/golangr/goroutines/2.go +++ b/golangr/goroutines/2.go @@ -8,6 +8,7 @@ import ( var numsToSum []int var choice2 int +var isAveraged bool func getAverage(arr []int, c chan int) { sum := 0 @@ -26,7 +27,11 @@ func statusMSG() { fmt.Printf("Current numbers: %v\n", numsToSum) fmt.Println("[1] Add number") fmt.Println("[2] Calculate average") - fmt.Println("[3] Exit") + fmt.Println("[3] Flush array") + fmt.Println("[4] Exit") + if isAveraged == true { + fmt.Println() + } } func getChoice2() { @@ -65,12 +70,13 @@ looper: // Send function call to goroutine while providing the channel for communication go getAverage(numsToSum, c) - fmt.Printf("\rThinking.") - time.Sleep(time.Second) - fmt.Printf("\rThinking..") - time.Sleep(time.Second) - fmt.Printf("\rThinking...") - time.Sleep(time.Second) + // This is far better than repeating myself, I can even expand the amount of dots it'll use! + thinkstr := "Thinking" + for i := 0; i < 4; i++ { + fmt.Printf("\r%v", thinkstr) + time.Sleep(time.Second) + thinkstr = thinkstr + "." + } fmt.Printf("\r\n") // Get the result returned by the goroutine @@ -81,6 +87,10 @@ looper: fmt.Printf("The average value is %v\n", average) time.Sleep(time.Second * 3) case 3: + fmt.Printf("\rThe array has been set to null") + numsToSum = nil + time.Sleep(time.Second) + case 4: break looper } } diff --git a/golangr/goroutines/main.go b/golangr/goroutines/main.go index 26c1faa..099658b 100644 --- a/golangr/goroutines/main.go +++ b/golangr/goroutines/main.go @@ -11,15 +11,19 @@ var check bool = false var choice string var err error +func mainStatus() { + clear() + fmt.Printf("[1] Basic concurrency\n") + fmt.Printf("[2] Channels\n") + fmt.Printf("[3] Exit\n") + fmt.Printf("\rChoose your function: ") +} + func main() { var numChoice int for { - clear() - fmt.Printf("[1] Basic concurrency\n") - fmt.Printf("[2] Channels\n") - - fmt.Printf("\rChoose your function: ") + mainStatus() strChoice := scanLine() numChoice, err = strconv.Atoi(strChoice) @@ -32,6 +36,8 @@ func main() { case 2: fun2() os.Exit(0) + case 3: + os.Exit(0) default: fmt.Printf("Choose an actual function, please\n") time.Sleep(time.Second)