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
+6.2k Golang : Dealing with backquote
+17.8k Golang : delete and modify XML file content
+13.5k Golang : Increment string example
+10.4k Golang : Convert file unix timestamp to UTC time example
+11.6k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+5.7k Golang : Shortening import identifier
+15.4k Golang : Get query string value on a POST request
+8.3k Golang : Reverse text lines or flip line order example
+51.5k Golang : Check if item is in slice/array
+8.6k PHP : How to parse ElasticSearch JSON ?
+11.5k Golang : Concatenate (combine) buffer data example
+5k Unix/Linux : secure copying between servers with SCP command examples