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
+20.2k Nginx + FastCGI + Go Setup.
+4.5k Linux/MacOSX : How to symlink a file?
+16.5k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+7.1k Golang : How to detect if a sentence ends with a punctuation?
+6.7k Golang : Decode XML data from RSS feed
+19.2k Golang : How to count the number of repeated characters in a string?
+11.2k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+11.1k Golang : Characters limiter example
+5.7k AWS S3 : Prevent Hotlinking policy
+9.8k Golang : Read file and convert content to string
+12.4k Golang : zlib compress file example
+9k Golang : Generate random Chinese, Japanese, Korean and other runes