Golang : Handling Yes No Quit query input
The following is a simple program that will accept a single key answer from the console without the user hitting the Enter
button and check if the answer is valid or not. Basically what it does is to prompt the user with a question and expect the user to answer Yes, No or Quit. Useful in situation where your program needs the user to give permission or customize/install certain modules.
Here you go!
package main
/*
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
char getch(){
char ch = 0;
struct termios old = {0};
fflush(stdout);
if( tcgetattr(0, &old) < 0 ) perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if( tcsetattr(0, TCSANOW, &old) < 0 ) perror("tcsetattr ICANON");
if( read(0, &ch,1) < 0 ) perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0) perror("tcsetattr ~ICANON");
return ch;
}
*/
import "C"
// stackoverflow.com/questions/14094190/golang-function-similar-to-getchar
import (
"fmt"
"math/rand" // to randomize the questions
"os"
"strings"
"time"
)
func main() {
validAnswers := map[string]string{"y": "yes", "n": "no", "q": "quit"}
questions := []string{"Do you want to make peace with the rest of the world?", "Terminate evil artificial intelligence program?", "Do you believe in a future that robot help us unconditionally?", "Promise to be a good boy?", "Spank them?"}
prompt := "[Yes/No/Quit]"
rand.Seed(time.Now().UnixNano())
for {
fmt.Println(questions[rand.Intn(len(questions))] + " " + prompt)
// get a single character from keyboard with hitting Enter button
// if you need your user to hit the Enter key
// see https://www.socketloop.com/tutorials/golang-get-input-from-keyboard
char := C.getch()
// convert type C.char to Golang string type
answer := fmt.Sprintf("%c", char)
answer = strings.ToLower(answer)
if validAnswers[answer] == "" {
fmt.Println("Please respond with y, n or q")
} else {
fmt.Println("Your answer is : ", validAnswers[answer])
}
if answer == "q" {
fmt.Println("Bye!")
os.Exit(0)
}
}
}
Sample output:
Spank them? [Yes/No/Quit]
Your answer is : yes
Do you believe in a future that robot help us unconditionally? [Yes/No/Quit]
Your answer is : no
Do you believe in a future that robot help us unconditionally? [Yes/No/Quit]
Your answer is : quit
Bye!
Do you want to make peace with the rest of the world? [Yes/No/Quit]
Your answer is : yes
Terminate evil artificial intelligence program? [Yes/No/Quit]
Your answer is : yes
Do you believe in a future that robot help us unconditionally? [Yes/No/Quit]
Your answer is : yes
Do you want to make peace with the rest of the world? [Yes/No/Quit]
Your answer is : yes
Do you want to make peace with the rest of the world? [Yes/No/Quit]
Your answer is : yes
Terminate evil artificial intelligence program? [Yes/No/Quit]
Your answer is : yes
Do you want to make peace with the rest of the world? [Yes/No/Quit]
Your answer is : yes
Promise to be a good boy? [Yes/No/Quit]
Your answer is : no
Terminate evil artificial intelligence program? [Yes/No/Quit]
Your answer is : quit
Bye!
References:
https://www.socketloop.com/tutorials/golang-check-if-element-exist-in-map
https://www.socketloop.com/tutorials/golang-randomly-pick-an-item-from-a-slice-array-example
https://www.socketloop.com/tutorials/golang-get-input-from-keyboard
See also : Golang : Get input from keyboard
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+18.3k Golang : Generate thumbnails from images
+5.5k Golang : Error handling methods
+7.7k Golang : Sort words with first uppercase letter
+10.9k CodeIgniter : How to check if a session exist in PHP?
+13.9k Golang : Check if a file exist or not
+24.2k Golang : Time slice or date sort and reverse sort example
+9.1k Golang : Terminate-stay-resident or daemonize your program?
+7.9k Golang : Get final or effective URL with Request.URL example
+14.3k Golang : Rename directory
+9.8k Golang : Get login name from environment and prompt for password
+20.8k Golang : Get password from console input without echo or masked