12.0-12.1

This commit is contained in:
Unknwon
2015-11-11 12:47:10 -05:00
parent 719824b26d
commit 522e63960b
6 changed files with 14 additions and 10 deletions

View File

@@ -9,7 +9,7 @@
## 翻译进度 ## 翻译进度
11.14 [结构体、集合和高阶函数](eBook/11.14.md) 12.1 [读取用户的输入](eBook/12.1.md)
## 支持本书 ## 支持本书

View File

@@ -30,4 +30,4 @@ Golang 编程245386165
|更新日期 |更新内容 |更新日期 |更新内容
|----------|------------------ |----------|------------------
|2015-11-10|11.14 结构体、集合和高阶函数 |2015-11-11|12.1 读取用户的输入

3
TOC.md
View File

@@ -103,3 +103,6 @@
- 11.12 [接口与动态类型](11.12.md) - 11.12 [接口与动态类型](11.12.md)
- 11.13 [总结Go 中的面向对象](11.13.md) - 11.13 [总结Go 中的面向对象](11.13.md)
- 11.14 [结构体、集合和高阶函数](11.14.md) - 11.14 [结构体、集合和高阶函数](11.14.md)
- 第三部分Go 高级编程
- 第12章[读写数据](12.0.md)
- 12.1 [读取用户的输入](12.1.md)

View File

@@ -5,5 +5,5 @@
## 链接 ## 链接
- [目录](directory.md) - [目录](directory.md)
- 上一章:[垃圾回收和SetFinalizer](10.8.md) - 上一章:[垃圾回收和 SetFinalizer](10.8.md)
- 下一节:[接口是什么](11.1.md) - 下一节:[接口是什么](11.1.md)

View File

@@ -5,5 +5,5 @@
## 链接 ## 链接
- [目录](directory.md) - [目录](directory.md)
- 上一章:[接口interface与反射reflection](11.0.md) - 上一章:[结构体、集合和高阶函数](11.14.md)
- 下一节:[读取用户的输入](12.1.md) - 下一节:[读取用户的输入](12.1.md)

View File

@@ -71,6 +71,7 @@ func main() {
屏幕是标准输出 `os.Stdout``os.Stderr` 用于显示错误信息,大多数情况下等同于 `os.Stdout` 屏幕是标准输出 `os.Stdout``os.Stderr` 用于显示错误信息,大多数情况下等同于 `os.Stdout`
一般情况下,我们会省略变量声明,而使用 `:=`,例如: 一般情况下,我们会省略变量声明,而使用 `:=`,例如:
```go ```go
inputReader := bufio.NewReader(os.Stdin) inputReader := bufio.NewReader(os.Stdin)
input, err := inputReader.ReadString('\n') input, err := inputReader.ReadString('\n')
@@ -81,6 +82,7 @@ input, err := inputReader.ReadString('\n')
第二个例子从键盘读取输入,使用了 `switch` 语句: 第二个例子从键盘读取输入,使用了 `switch` 语句:
示例 12.3 [switch_input.go](examples/chapter_12/switch_input.go) 示例 12.3 [switch_input.go](examples/chapter_12/switch_input.go)
```go ```go
package main package main
import ( import (
@@ -93,12 +95,12 @@ func main() {
inputReader := bufio.NewReader(os.Stdin) inputReader := bufio.NewReader(os.Stdin)
fmt.Println("Please enter your name:") fmt.Println("Please enter your name:")
input, err := inputReader.ReadString('\n') input, err := inputReader.ReadString('\n')
if err != nil { if err != nil {
fmt.Println("There were errors reading, exiting program.") fmt.Println("There were errors reading, exiting program.")
return return
} }
fmt.Printf("Your name is %s", input) fmt.Printf("Your name is %s", input)
// For Unix: test with delimiter "\n", for Windows: test with "\r\n" // For Unix: test with delimiter "\n", for Windows: test with "\r\n"
switch input { switch input {
@@ -107,7 +109,7 @@ func main() {
case "Ivo\r\n": fmt.Println("Welcome Ivo!") case "Ivo\r\n": fmt.Println("Welcome Ivo!")
default: fmt.Printf("You are not welcome here! Goodbye!") default: fmt.Printf("You are not welcome here! Goodbye!")
} }
// version 2: // version 2:
switch input { switch input {
case "Philip\r\n": fallthrough case "Philip\r\n": fallthrough
@@ -115,7 +117,7 @@ func main() {
case "Chris\r\n": fmt.Printf("Welcome %s\n", input) case "Chris\r\n": fmt.Printf("Welcome %s\n", input)
default: fmt.Printf("You are not welcome here! Goodbye!\n") default: fmt.Printf("You are not welcome here! Goodbye!\n")
} }
// version 3: // version 3:
switch input { switch input {
case "Philip\r\n", "Ivo\r\n": fmt.Printf("Welcome %s\n", input) case "Philip\r\n", "Ivo\r\n": fmt.Printf("Welcome %s\n", input)
@@ -137,11 +139,10 @@ iii) 输入的行数
**练习 12.2:** [calculator.go](exercises/chapter_12/calculator.go) **练习 12.2:** [calculator.go](exercises/chapter_12/calculator.go)
编写一个简单的逆波兰式计算器,它接受用户输入的整型数(最大值 999999和运算符 +、-、*、/。 编写一个简单的逆波兰式计算器,它接受用户输入的整型数(最大值 999999和运算符 +、-、\*、/。
输入的格式为number1 ENTER number2 ENTER operator ENTER --> 显示结果 输入的格式为number1 ENTER number2 ENTER operator ENTER --> 显示结果
当用户输入字符 'q' 时程序结束。请使用您在练习11.3中开发的 `stack` 包。 当用户输入字符 'q' 时程序结束。请使用您在练习11.3中开发的 `stack` 包。
## 链接 ## 链接
- [目录](directory.md) - [目录](directory.md)