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
+10.5k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+11.3k Golang : How to use if, eq and print properly in html template
+10.2k Golang : Text file editor (accept input from screen and save to file)
+7.7k Gogland : Where to put source code files in package directory for rookie
+9.7k Golang : interface - when and where to use examples
+13.6k Golang : Qt progress dialog example
+13.7k Golang : Image to ASCII art example
+7.3k Golang : Example of custom handler for Gorilla's Path usage.
+7.8k Golang : Regular Expression find string example
+32.7k Golang : Regular Expression for alphanumeric and underscore
+4.9k Golang : Calculate a pip value and distance to target profit example