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
+4.7k Linux : sudo yum updates not working
+7.5k Android Studio : How to detect camera, activate and capture example
+6k AWS S3 : Prevent Hotlinking policy
+5.2k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+33.8k Golang : convert(cast) bytes to string
+4.7k MariaDB/MySQL : How to get version information
+9.2k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+16k Golang : Get file permission
+12.9k Golang : Convert IPv4 address to packed 32-bit binary format
+52.7k Golang : How to get struct field and value by name
+8.3k Golang : Reverse text lines or flip line order example