package main
import (
"bufio"
"fmt"
"os"
)
func Option1() {
fmt.Println("Option1")
}
func main() {
for true {
fmt.Println("Pleae enter text: ")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
if text == "1" {
Option1()
}
}
}
So my code will eventually have multi functions example: option1 option2 and etc.. I'm trying to execute a function every time a user types 1, 2, 3 and etc, but my code doesn't print out whatever is in Option1.
Your help will be appreciated.
\nis included in the value returned by ReadString. In addition, the program discards data buffered from stdin. Use a Scanner instead. See this question for more info.text[:1].for { fmt.Println(prompt); if !scanner.Scan() { break }; text := scanner.Text(); /* do something with text */}