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
+14k Golang : Compress and decompress file with compress/flate example
+7.4k Golang : Convert source code to assembly language
+4.8k MariaDB/MySQL : Form select statement or search query with Chinese characters
+18.4k Golang : How to remove certain lines from a file
+6.9k Android Studio : Hello World example
+34.6k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+8.2k Prevent Write failed: Broken pipe problem during ssh session with screen command
+5.1k Golang : Check if a word is countable or not
+8.5k Golang : Generate Datamatrix barcode
+9.2k Golang : Generate Codabar
+15k Golang : Search folders for file recursively with wildcard support
+5.4k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents