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
+5.9k Linux : Disable and enable IPv4 forwarding
+6.1k PHP : How to check if an array is empty ?
+16.5k Golang : Send email and SMTP configuration example
+16.8k Golang : Gzip file example
+10.3k Golang : How to get quoted string into another string?
+6.5k CodeIgniter : form input set_value cause " to become & quot
+7.7k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+9.3k Golang : How to check if a string with spaces in between is numeric?
+6.9k Golang : Find the longest line of text example
+28.7k Golang : Read, Write(Create) and Delete Cookie example
+8.1k Golang : Sort words with first uppercase letter
+12k Golang : Convert decimal number(integer) to IPv4 address