golang-exercises/golangr/goroutines/2.go

64 lines
1.0 KiB
Go
Raw Normal View History

2024-03-05 19:57:16 +01:00
package main
import (
"fmt"
"strconv"
"time"
2024-03-05 19:57:16 +01:00
)
var numsToSum []int
var choice2 int
// TODO: Get function done
func getAverage() {
}
func addToVec(num int, c chan int) {
numsToSum = append(numsToSum, num)
}
2024-03-05 19:57:16 +01:00
func statusMSG() {
fmt.Printf("Current numbers: %v\n", numsToSum)
fmt.Println("[1] Add number")
fmt.Println("[2] Calculate average")
fmt.Println("[3] Exit")
2024-03-05 19:57:16 +01:00
}
func getChoice2() {
2024-03-05 19:57:16 +01:00
strchoice2 := scanLine()
choice2, err = strconv.Atoi(strchoice2)
catchErr(err)
}
2024-03-05 19:57:16 +01:00
func getNumToAdd() (num int) {
fmt.Printf("Select your number: ")
strnum := scanLine()
num, err = strconv.Atoi(strnum)
return num
}
func fun2() {
c := make(chan int)
2024-03-05 19:57:16 +01:00
looper:
for {
clear()
statusMSG()
fmt.Printf("Choice: ")
getChoice2()
2024-03-05 19:57:16 +01:00
switch choice2 {
// TODO: Add options to add numbers to array and then to calcluate them later
case 1:
numToAdd := getNumToAdd()
addToVec(numToAdd, c)
fmt.Printf("Succesfully added %v to the array\n", numToAdd)
time.Sleep(time.Second)
case 2:
getAverage()
case 3:
2024-03-05 19:57:16 +01:00
break looper
}
}
}