|
|
@@ -56,6 +56,94 @@ func main() {
|
|
|
}
|
|
|
```
|
|
|
|
|
|
+`inputReader` 是一个指向 `bufio.Reader` 的指针。`inputReader := bufio.NewReader(os.Stdin)` 这行代码,将会创建一个读取器,并将其与标准输入绑定。
|
|
|
+
|
|
|
+`bufio.NewReader()` 构造函数的签名为:`func NewReader(rd io.Reader) *Reader`
|
|
|
+
|
|
|
+该函数的实参可以是满足 `io.Reader` 接口的任意对象(任意包含有适当的 `Read()` 方法的对象,请参考[章节11.8](11.8.md)),函数返回一个新的带缓冲的 `io.Reader` 对象,它将从指定读取器(例如 `os.Stdin`)读取内容。
|
|
|
+
|
|
|
+返回的读取器对象提供一个方法 `ReadString(delim byte)`,该方法从输入中读取内容,直到碰到 `delim` 指定的字符,然后将读取到的内容连同 `delim` 字符一起放到缓冲区。
|
|
|
+
|
|
|
+`ReadString` 返回读取到的字符串,如果碰到错误则返回 `nil`。如果它一直读到文件结束,则返回读取到的字符串和 `io.EOF`。如果读取过程中没有碰到 `delim` 字符,将返回错误 `err != nil`。
|
|
|
+
|
|
|
+在上面的例子中,我们会读取键盘输入,直到回车键(\n)被按下。
|
|
|
+
|
|
|
+屏幕是标准输出 `os.Stdout`;`os.Stderr` 用于显示错误信息,大多数情况下等同于 `os.Stdout`。
|
|
|
+
|
|
|
+一般情况下,我们会省略变量声明,而使用 `:=`,例如:
|
|
|
+```go
|
|
|
+inputReader := bufio.NewReader(os.Stdin)
|
|
|
+input, err := inputReader.ReadString('\n')
|
|
|
+```
|
|
|
+
|
|
|
+我们将从现在开始使用这种写法。
|
|
|
+
|
|
|
+第二个例子从键盘读取输入,使用了 `switch` 语句:
|
|
|
+
|
|
|
+**Listing 12.3—switch_input.go:**
|
|
|
+
|
|
|
+```go
|
|
|
+package main
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "os"
|
|
|
+ "bufio"
|
|
|
+)
|
|
|
+
|
|
|
+func main() {
|
|
|
+ inputReader := bufio.NewReader(os.Stdin)
|
|
|
+ fmt.Println("Please enter your name:")
|
|
|
+ input, err := inputReader.ReadString('\n')
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("There were errors reading, exiting program.")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Printf("Your name is %s", input)
|
|
|
+ // For Unix: test with delimiter "\n", for Windows: test with "\r\n"
|
|
|
+ switch input {
|
|
|
+ case "Philip\r\n": fmt.Println("Welcome Philip!")
|
|
|
+ case "Chris\r\n": fmt.Println("Welcome Chris!")
|
|
|
+ case "Ivo\r\n": fmt.Println("Welcome Ivo!")
|
|
|
+ default: fmt.Printf("You are not welcome here! Goodbye!")
|
|
|
+ }
|
|
|
+
|
|
|
+ // version 2:
|
|
|
+ switch input {
|
|
|
+ case "Philip\r\n": fallthrough
|
|
|
+ case "Ivo\r\n": fallthrough
|
|
|
+ case "Chris\r\n": fmt.Printf("Welcome %s\n", input)
|
|
|
+ default: fmt.Printf("You are not welcome here! Goodbye!\n")
|
|
|
+ }
|
|
|
+
|
|
|
+ // version 3:
|
|
|
+ switch input {
|
|
|
+ case "Philip\r\n", "Ivo\r\n": fmt.Printf("Welcome %s\n", input)
|
|
|
+ default: fmt.Printf("You are not welcome here! Goodbye!\n")
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+注意:Unix和Windows的行结束符是不同的!
|
|
|
+
|
|
|
+**练习**
|
|
|
+
|
|
|
+**Exercise 12.1:** word_letter_count.go
|
|
|
+Write a program which reads text from the keybord. When the user enters ‘S’ in order to signal the end of the input, the program shows 3 numbers:
|
|
|
+i) the number of characters including spaces (but excluding ‘\r’ and ‘\n’)
|
|
|
+ii) the number of words
|
|
|
+iii) the number of lines
|
|
|
+
|
|
|
+**Exercise 12.2:** calculator.go
|
|
|
+Make a simple (reverse polish notation) calculator. This program accepts input from the user in the
|
|
|
+form of integers (maximum 999999) and operators (+, -, *, /).
|
|
|
+
|
|
|
+The input is like this: number1 ENTER number2 ENTER operator ENTER result is displayed.
|
|
|
+
|
|
|
+The programs stops if the user inputs “q”. Use the package stack you developed in Ex. 11.3
|
|
|
+
|
|
|
+
|
|
|
## 链接
|
|
|
|
|
|
- [目录](directory.md)
|