Golang : Get user input until a command or receive a word to stop
Just a code fragment below as supplement to tutorial on how to read until certain character to break loop. In this tutorial, the program will continue reading from the console(terminal) until a string that starts with bye
.
Here you go :
// run forever until user issue bye
for {
consoleReader := bufio.NewReader(os.Stdin)
fmt.Print(">")
input, _ := consoleReader.ReadString('\n')
input = strings.ToLower(input)
if strings.HasPrefix(input, "bye") {
fmt.Println("Good bye!")
os.Exit(0)
}
}
See also : Golang : Read until certain character to break for loop
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
+17.4k Golang : [json: cannot unmarshal object into Go value of type]
+7.3k Golang : How to stop user from directly running an executable file?
+9.6k PHP : Get coordinates latitude/longitude from string
+13.5k Generate salted password with OpenSSL example
+13.7k Golang : How to check if a file is hidden?
+9.7k Golang : Get current, epoch time and display by year, month and day
+20.3k Golang : Secure(TLS) connection between server and client
+10.4k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+13.2k Golang : Generate Code128 barcode
+4.6k Unix/Linux : How to pipe/save output of a command to file?
+6.1k Apt-get to install and uninstall Golang
+12.4k Golang : Exit, terminating or aborting a program