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
+36.5k Golang : Smarter Error Handling with strings.Contains()
+9k Golang : On lambda, anonymous, inline functions and function literals
+39.4k Golang : How to read CSV file
+5.4k JavaScript/JQuery : Redirect page examples
+26.2k Golang : How to read integer value from standard input ?
+15.4k Golang : Save(pipe) HTTP response into a file
+7k Get Facebook friends working in same company
+11.3k Golang : Calculate Relative Strength Index(RSI) example
+23.1k Golang : Calculate time different
+7.5k Golang : Check if one string(rune) is permutation of another string(rune)
+8.4k Golang : Oanda bot with Telegram and RSI example
+48.8k Golang : Upload file from web browser to server