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
+12.4k Golang : Verify token from Google Authenticator App
+2.2k Golang : Fix go-cron set time not working issue
+7.4k Useful methods to access blocked websites
+14.1k Golang : Find location by IP address and display with Google Map
+20.7k Golang : Join arrays or slices example
+5k Unix/Linux : How to open tar.gz file ?
+6.7k Gogland : Single File versus Go Application Run Configurations
+12.2k Swift : Convert (cast) Int to String ?
+29.5k Golang : Interpolating or substituting variables in string examples
+7.2k Prevent Write failed: Broken pipe problem during ssh session with screen command
+10k Golang : Generate random elements without repetition or duplicate
+12.9k Golang : How to convert a number to words