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
+6.3k PHP : Shuffle to display different content or advertisement
+18.1k Golang : Aligning strings to right, left and center with fill example
+19.6k Golang : How to get time from unix nano example
+7.9k Golang : Randomize letters from a string example
+10.3k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+8.1k Golang : Implementing class(object-oriented programming style)
+8.6k Golang : Find network service name from given port and protocol
+25.5k Golang : How to read integer value from standard input ?
+24.7k Golang : Create PDF file from HTML file
+26.4k Golang : Force your program to run with root permissions
+13.3k Generate salted password with OpenSSL example
+9.3k Golang : How to generate Code 39 barcode?