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.2k Golang : Select region of interest with mouse click and crop from image
+10.2k Generate Random number with math/rand in Go
+31.1k Golang : Get local IP and MAC address
+13.1k Golang : Generate Code128 barcode
+7.2k Golang : Handling Yes No Quit query input
+6.9k Golang : Dealing with postal or zip code example
+13.2k Golang : Get user input until a command or receive a word to stop
+6.8k Golang : Validate credit card example
+12.1k Elastic Search : Return all records (higher than default 10)
+11.8k Golang : Perform sanity checks on filename example
+5.8k Golang : Convert Chinese UTF8 characters to Pin Yin
+7.3k Golang : Convert(cast) io.Reader type to string