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
+9.4k Golang : Load ASN1 encoded DSA public key PEM file example
+7.8k Golang : Metaprogramming example of wrapping a function
+6.3k Golang : Check if password length meet the requirement
+9.1k Golang : Changing a RGBA image number of channels with OpenCV
+15.7k Golang : Loop each day of the current month example
+6.9k Golang : Dealing with postal or zip code example
+30.4k error: trying to remove "yum", which is protected
+8.5k Golang : Get final balance from bit coin address example
+14.8k Golang : How to get Unix file descriptor for console and file
+14.5k Golang : Basic authentication with .htpasswd file
+8.2k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+26.3k Golang : How to check if a connection to database is still alive ?