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
+7.5k Golang : Characters limiter example
+6.2k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+30.8k Golang : Upload and download file to/from AWS S3
+15.2k Golang : Simple client server example
+9.1k Golang : Display list of countries and ISO codes
+37.3k Golang : Use wildcard patterns with filepath.Glob() example
+17.4k Golang : Delete item from slice based on index/key position
+7.8k Golang : Fix go.exe is not compatible with the version of Windows you're running
+4.5k Golang : Skip or discard items of non-interest when iterating example
+13.3k Golang : File path independent of Operating System
+7.5k Golang : Copy map(hash table) example
+3.5k Python : Create Whois client or function example