diff --git a/golangr/goroutines/1.go b/golangr/goroutines/1.go index f397197..b4bce35 100644 --- a/golangr/goroutines/1.go +++ b/golangr/goroutines/1.go @@ -9,7 +9,7 @@ import ( func bgChecker() { for { if check == true { - fmt.Println("Check has been set to true, quitting") + fmt.Println("\nCheck has been set to true, quitting") os.Exit(0) } time.Sleep(time.Second) diff --git a/golangr/goroutines/main.go b/golangr/goroutines/main.go index 6e30f5a..093b51d 100644 --- a/golangr/goroutines/main.go +++ b/golangr/goroutines/main.go @@ -2,23 +2,34 @@ package main import ( "fmt" + "os" + "strconv" ) var check bool = false var choice string +var err error func main() { var numChoice int for { fmt.Printf("[1] Basic concurrency\n") + fmt.Printf("[2] Channels\n") fmt.Printf("\rChoose your function: ") - fmt.Scanln(&numChoice) + + strChoice := scanLine() + numChoice, err = strconv.Atoi(strChoice) + catchErr(err) switch numChoice { case 1: fun1() + os.Exit(0) + case 2: + fun2() + os.Exit(0) default: fmt.Printf("Choose an actual function, please\n") } diff --git a/golangr/goroutines/scanLine.go b/golangr/goroutines/scanLine.go new file mode 100644 index 0000000..4ce8375 --- /dev/null +++ b/golangr/goroutines/scanLine.go @@ -0,0 +1,35 @@ +package main + +import ( + "bufio" + "os" + "runtime" + "strings" +) + +func scanLine() (line string) { + switch runtime.GOOS { + case "linux": + in := bufio.NewReader(os.Stdin) + lineNew, err := in.ReadString('\n') + catchErr(err) + line = strings.Trim(lineNew, "\n") + + // I hate Windows + case "windows": + in := bufio.NewReader(os.Stdin) + lineNew, err := in.ReadString('\r') + catchErr(err) + line = strings.Trim(lineNew, "\r") + } + return line +} + +func catchErr(err error) (errHappened bool) { + errHappened = false + if err != nil { + //fmt.Println(err) + errHappened = true + } + return errHappened +}