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
+4.9k Golang : Gonum standard normal random numbers example
+58.3k Golang : How to convert character to ASCII and back
+9.7k Golang : Overwrite previous output with count down timer
+9.8k Golang : Find commonalities in two slices or arrays example
+10.1k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+3.9k Golang : How to determine if request or crawl is from Google robots
+10.7k Golang : Check if an integer is negative or positive
+4.9k Golang : How to execute code at certain day, hour and minute?
+5.7k Golang : Gaussian blur on image and camera video feed examples
+2.8k Golang : micron to centimeter example
+23.5k Golang : Get and Set User-Agent examples
+29.1k Golang : Upload and download file to/from AWS S3