1 回答
TA贡献1795条经验 获得超7个赞
使用bufio.Scanner一个os.Stdin:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
readLine := func() (ln string, ok bool) {
for {
fmt.Print("? ")
if ok = scanner.Scan(); !ok {
break
}
if ln = scanner.Text(); ln != "" {
break
}
fmt.Println("You didn't enter any text.")
}
return ln, ok
}
fmt.Println("Please enter some text at the prompt. Type 'exit' to quit.")
for ln, ok := readLine(); ok; ln, ok = readLine() {
fmt.Printf("You entered: %q\n", ln)
if ln == "exit" {
break
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
fmt.Println("Goodbye!")
}
- 1 回答
- 0 关注
- 81 浏览
添加回答
举报