Translating An Introduction to Go.

This commit is contained in:
LazyWolf Lin 2019-01-17 13:29:16 +08:00
parent 4ba64b2486
commit a42facff91

View File

@ -119,24 +119,21 @@ type MyMethoder interface {
### 控制流
Go provides three primary statements for control flow: `if`, `switch`, and `for`. The statements are fairly similar to their equivalent in other C-like languages, with some exceptions:
Go 提供了三个主要的控制了语句:`if`、`switch` 和 `for`。这些语句同其他 C 风格语言内的语句非常类似,但有一些不同:
* There are no parentheses around conditions, so it is `if a == b {}`, not `if (a == b) {}`. The braces are mandatory.
* 条件语句没有括号,所以条件语句是 `if a == b {}` 而不是 `if (a == b) {}`。大括号是必须的。
* All of them can have initialisers, like this
`if result, err := someFunction(); err == nil { // use result }`
* The `switch` statement can use arbitrary expressions in cases
* The `switch` statement can switch over nothing (equals switching over true)
* Cases do not fall through by default (no `break` needed), use `fallthrough` at the end of a block to fall through.
* The `for` loop can loop over ranges: `for key, val := range map { do something }`
* 所有的语句都可以有初始化,比如这个
`if result, err := someFunction(); err == nil { // use result }`
* `switch` 语句在 cases 里可以使用任何表达式
* `switch` 语句可以处理空的表达式(等于 true
* 默认情况下Go 不会从一个 case 进入下一个 case不需要 `break`),在程序块的末尾使用 `fallthrough` 则会进入下一个 case。
* 循环语句 `for` 不止能循环值域:`for key, val := range map { do something }`
### Goroutines