package main import ( "fmt" "strconv" "time" ) var numsToSum []int var choice2 int var isAveraged bool func getAverage(arr []int, c chan int) { sum := 0 for i := 0; i < len(arr); i++ { sum = sum + arr[i] } c <- sum } func addToVec(num int) { numsToSum = append(numsToSum, num) } func statusMSG() { fmt.Printf("Current numbers: %v\n", numsToSum) fmt.Println("[1] Add number") fmt.Println("[2] Calculate average") fmt.Println("[3] Flush array") fmt.Println("[4] Exit") if isAveraged == true { fmt.Println() } } func getChoice2() { strchoice2 := scanLine() choice2, err = strconv.Atoi(strchoice2) catchErr(err) } func getNumToAdd() (num int) { fmt.Printf("Select your number: ") strnum := scanLine() num, err = strconv.Atoi(strnum) return num } func fun2() { // Create channel made for int values c := make(chan int) looper: for { clear() statusMSG() fmt.Printf("Choice: ") getChoice2() switch choice2 { case 1: numToAdd := getNumToAdd() addToVec(numToAdd) fmt.Printf("Succesfully added %v to the array\n", numToAdd) time.Sleep(time.Second) case 2: // Send function call to goroutine while providing the channel for communication go getAverage(numsToSum, c) // 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 total := <-c average := total / len(numsToSum) fmt.Printf("The grand total is %v\n", total) 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 } } }