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.7k Golang : Add build version and other information in executables
+7.5k Golang : Simple histogram example
+6.1k Golang : What fmt.Println() can do and println() cannot do
+10.3k Golang : Simple client-server HMAC authentication without SSL example
+5.1k Golang : Break string into a slice of characters example
+23.3k Golang : Storing cookies in http.CookieJar example
+8.5k Golang : Translate language with language package example
+4.5k Golang : How to search a list of records or data structures
+7.6k Golang : Generate EAN barcode
+7.3k Golang : Convert(cast) []byte to io.Reader type
+8.1k Golang : Convert(cast) string to int64