Skip to content

Instantly share code, notes, and snippets.

@KaiserWerk
Created May 20, 2020 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KaiserWerk/a703a43172a286a9a6cf1edf73cda4d1 to your computer and use it in GitHub Desktop.
Save KaiserWerk/a703a43172a286a9a6cf1edf73cda4d1 to your computer and use it in GitHub Desktop.
Golang read console input in a non-blocking way
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