Created
May 20, 2020 08:33
-
-
Save KaiserWerk/a703a43172a286a9a6cf1edf73cda4d1 to your computer and use it in GitHub Desktop.
Golang read console input in a non-blocking way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go func() { | |
reader := bufio.NewReader(os.Stdin) | |
for { | |
input, _, err := reader.ReadLine() | |
if err != nil { | |
fmt.Printf("could not process input %v\n", input) | |
} | |
switch string(input) { | |
case "hello": | |
fmt.Println("hello world") | |
case "moo": | |
moo := ` (__) | |
(oo) | |
/------\/ | |
/ | || | |
* /\---/\ | |
~~ ~~ | |
..."Have you mooed today?"...` | |
// powered by apt-get moo | |
fmt.Println(moo) | |
case "reload config": | |
fmt.Printf("configuration reloaded") | |
case "check connectivity": | |
fmt.Println("internet check") | |
case "check license": | |
fmt.Println("check license") | |
default: | |
fmt.Printf("unrecognized command: %v\n", string(input)) | |
} | |
} | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment