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.1k Golang : Embed secret text string into binary(executable) file
+22.8k Golang : Daemonizing a simple web server process example
+4.3k Golang : The Tao of importing package
+7.8k Golang : Get final balance from bit coin address example
+4.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+8.8k PHP : Get coordinates latitude/longitude from string
+22.2k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+6.8k Golang : Error reading timestamp with GORM or SQL driver
+25.2k Golang : Find files by extension
+5.2k Golang : How to get capacity of a slice or array?
+10k Golang : Characters limiter example
+3.9k Unix/Linux : How to pipe/save output of a command to file?