Improved UX by reusing scanLine function
This commit is contained in:
parent
a89ca3987b
commit
9e978f0bcf
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue