从stdin读取命令
package main
import (
"os"
"fmt"
"bufio"
"strings"
)
func main() {
stop := false
for !stop {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
switch cmd := strings.TrimSuffix(text, "\n"); cmd {
case "q", "quit":
stop = true
case "hello":
fmt.Println("Hello World")
default:
fmt.Println(cmd)
}
}
}
运行
$ go build main.go && ./main
Enter text: aaa
aaa
Enter text: hello
Hello World
Enter text: q
网友评论