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
+5.6k Golang : Frobnicate or tweaking a string example
+6.5k Golang : Convert an executable file into []byte example
+7.8k Golang Hello World Example
+18k Golang : Convert IPv4 address to decimal number(base 10) or integer
+5.8k Golang : Detect variable or constant type
+12.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+30.5k Golang : Remove characters from string example
+22k Golang : Match strings by wildcard patterns with filepath.Match() function
+7.3k Golang : Scanf function weird error in Windows
+5.7k Unix/Linux : How to test user agents blocked successfully ?
+15.5k Golang : rune literal not terminated error
+16.2k Golang : convert string or integer to big.Int type