Add:04-statements-expressions Chinese version
This commit is contained in:
@ -0,0 +1,23 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello!")
|
||||
|
||||
// 语句改变执行顺序
|
||||
// 特别是流程控制语句,如 `if`
|
||||
if 5 > 1 {
|
||||
fmt.Println("bigger")
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello"); fmt.Println("World!")
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello!" + "!")
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// runtime.NumCPU() 是一个调用表达式
|
||||
fmt.Println(runtime.NumCPU() + 1)
|
||||
}
|
22
translation/chinese/04-语句-表达式-注释/03-comments/main.go
Normal file
22
translation/chinese/04-语句-表达式-注释/03-comments/main.go
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
// Package main makes this package an executable program
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
/*
|
||||
main 函数
|
||||
Go 通过这个函数执行这个程序
|
||||
在 main package 有且只有一个 main 函数
|
||||
可执行程序也可以称为 `commands`
|
||||
*/
|
||||
func main() {
|
||||
fmt.Println("Hello Gopher!")
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 练习: 害羞的分号
|
||||
//
|
||||
// 1. 尝试使用分号来分隔表达式
|
||||
//
|
||||
//
|
||||
// 2. 观察 Go 如何修复它们
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// 取消下面代码行的注释,然后保存文件。
|
||||
//
|
||||
// 可以看到 Gofmt 工具将自动格式化代码。
|
||||
// https://golang.org/cmd/gofmt/
|
||||
//
|
||||
// 这是因为,Go 语言并不关心
|
||||
// 在语句之间是否使用分号
|
||||
//
|
||||
// 毕竟,它会自动添加它们
|
||||
|
||||
/*
|
||||
fmt.Println("inanc"); fmt.Println("lina"); fmt.Println("ebru");
|
||||
*/
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 练习: 裸表达式
|
||||
//
|
||||
// 1. 尝试单独输入 "Hello" 在一行
|
||||
// 2. 不要使用 Println
|
||||
// 3. 查看错误
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ?
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// 取消下面一行的代码的注释,以查看错误。
|
||||
|
||||
/*
|
||||
"Hello"
|
||||
*/
|
||||
|
||||
// 会显示: "evaluted but not used"
|
||||
//
|
||||
// 因为:
|
||||
// "Hello" 字面值返回了一个值但是
|
||||
// 没有语句使用它
|
||||
//
|
||||
// 因此:
|
||||
// 不能单独使用表达式而不使用语句
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 练习: 运算符组合表达式
|
||||
//
|
||||
// 输出下面的期待的输出结果
|
||||
// 使用字符串连接运算符
|
||||
//
|
||||
// 提示
|
||||
// 使用 + 运算符多次来组成 "Hello!!!?".
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// "Hello!!!?"
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
|
||||
// fmt.Println("Hello!" + ?)
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// 操作符可以将多个表达式
|
||||
// 连接成一个表达式
|
||||
fmt.Println("Hello!" + "!" + "!" + "?")
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 练习: 打印 Go 版本号
|
||||
//
|
||||
// 1. 查看 runtime 包的文档
|
||||
// 2. 寻找返回 Go 版本号的函数
|
||||
// 3. 打印 Go 版本号通过调用这个函数
|
||||
//
|
||||
// 提示
|
||||
// 文档地址: https://golang.org/pkg/runtime
|
||||
//
|
||||
// 期待输出
|
||||
// "go1.10"
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// ?
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(runtime.Version())
|
||||
}
|
27
translation/chinese/04-语句-表达式-注释/练习/05-comment-out/main.go
Normal file
27
translation/chinese/04-语句-表达式-注释/练习/05-comment-out/main.go
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// 练习: 注释掉它
|
||||
//
|
||||
// 使用单行注释和多行注释注释掉下面的输出
|
||||
//
|
||||
// 期待输出
|
||||
// 没有输出
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
fmt.Println("hello")
|
||||
fmt.Println("how")
|
||||
fmt.Println("are")
|
||||
fmt.Println("you")
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
// For more tutorials : https://learngoprogramming.com
|
||||
// In-person training : https://www.linkedin.com/in/inancgumus/
|
||||
// Follow me on twitter: https://twitter.com/inancgumus
|
||||
|
||||
package main
|
||||
|
||||
func main() {
|
||||
// fmt.Println("hello")
|
||||
/*
|
||||
fmt.Println("how")
|
||||
fmt.Println("are")
|
||||
fmt.Println("you")
|
||||
*/
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
## 练习
|
||||
|
||||
- 在命令行中打印 `runtime.NumCPU` 函数的文档
|
||||
- 在命令行中打印 `runtime.NumCPU` 函数的源码
|
||||
|
||||
## 提示
|
||||
|
||||
需要正确使用 `go doc` 工具
|
@ -0,0 +1,7 @@
|
||||
## 文档:
|
||||
|
||||
go doc runtime NumCPU
|
||||
|
||||
## 源代码:
|
||||
|
||||
go doc -src runtime NumCPU
|
23
translation/chinese/04-语句-表达式-注释/练习/README.md
Normal file
23
translation/chinese/04-语句-表达式-注释/练习/README.md
Normal file
@ -0,0 +1,23 @@
|
||||
1. **[害羞的分号](https://github.com/inancgumus/learngo/tree/master/04-statements-expressions-comments/exercises/01-shy-semicolons)**
|
||||
|
||||
观察 Go 是怎么处理分号
|
||||
|
||||
2. **[裸表达式](https://github.com/inancgumus/learngo/tree/master/04-statements-expressions-comments/exercises/02-naked-expression)**
|
||||
|
||||
观察使用不带语句的表达式时发生的情况
|
||||
|
||||
3. **[运算符组合表达式](https://github.com/inancgumus/learngo/tree/master/04-statements-expressions-comments/exercises/03-operators-combine)**
|
||||
|
||||
使用运算符组合表达式
|
||||
|
||||
4. **[打印 Go 版本号](https://github.com/inancgumus/learngo/tree/master/04-statements-expressions-comments/exercises/04-print-go-version)**
|
||||
|
||||
使用 Go 标准库中的软件包打印当前系统(当前运行 Go 程序的系统)安装的 Go 版本号
|
||||
|
||||
5. **[注释掉它](https://github.com/inancgumus/learngo/tree/master/04-statements-expressions-comments/exercises/05-comment-out)**
|
||||
|
||||
了解如何注释代码
|
||||
|
||||
6. **[使用 GoDoc](https://github.com/inancgumus/learngo/tree/master/04-statements-expressions-comments/exercises/06-use-godoc)**
|
||||
|
||||
尝试使用 Godoc
|
119
translation/chinese/04-语句-表达式-注释/问题/01-statements/README.md
Normal file
119
translation/chinese/04-语句-表达式-注释/问题/01-statements/README.md
Normal file
@ -0,0 +1,119 @@
|
||||
## 下面关于语句的描述哪个是正确的?
|
||||
1. 一个语句指示 Go 执行某些操作 *正确*
|
||||
2. 一个语句产生一个值
|
||||
3. 一个语句无法改变执行流
|
||||
|
||||
> **2:** 一个语句不能产生一个值,但它可以间接的帮助产生一个值
|
||||
>
|
||||
>
|
||||
> **3:** 不,它可以
|
||||
>
|
||||
>
|
||||
|
||||
|
||||
## 下面关于 Go 代码的执行顺序哪个是正确的?
|
||||
1. 从左到右
|
||||
2. 从上到下 *正确*
|
||||
3. 从右到左
|
||||
4. 从下到上
|
||||
|
||||
> **2:** 是的,Go 的执行顺序是从上到下,一次一个语句
|
||||
>
|
||||
>
|
||||
|
||||
|
||||
## 下面关于表达式的描述哪个是正确的?
|
||||
1. 表达式指示 Go 执行某些操作
|
||||
2. 一个表达式产生一个值 *正确*
|
||||
3. 表达式可以改变执行流
|
||||
|
||||
> **1:** 不,语句可以
|
||||
>
|
||||
>
|
||||
> **3:** 不,只有语句可以
|
||||
>
|
||||
>
|
||||
|
||||
|
||||
## 下面关于运算符的描述哪个是正确的?
|
||||
1. 运算符指示 Go 执行某些操作
|
||||
2. 运算符可以改变执行流
|
||||
3. 运算符可以组合表达式 *正确*
|
||||
|
||||
> **1:** 不,语句可以
|
||||
>
|
||||
>
|
||||
> **2:** 不,只有语句可以
|
||||
>
|
||||
>
|
||||
|
||||
|
||||
## 下面的程序为什么不起作用?
|
||||
```go
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
"Hello"
|
||||
}
|
||||
```
|
||||
|
||||
1. "Hello"是一个表达式,它不能没有语句单独在一行 *正确*
|
||||
2. 去掉 “Hello” 的双引号,像这样: Hello
|
||||
3. 将 "Hello" 移到 main 函数外面
|
||||
|
||||
|
||||
## 下面的程序可以运行吗?
|
||||
```go
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(runtime.NumCPU()); fmt.Println("cpus"); fmt.Println("the machine")
|
||||
}
|
||||
```
|
||||
|
||||
1. 可以运行: 可以用分号来分隔表达式
|
||||
2. 不能运行: 只能一行一个语句
|
||||
3. 可以运行: Go 会自动地在每条语句的后面添加分号 *正确*
|
||||
|
||||
> **1:** 它可以运行,但不是这个原因,另外,表达式不能这样做
|
||||
>
|
||||
>
|
||||
> **2:** 确定吗?
|
||||
>
|
||||
>
|
||||
> **3:** 是的,无论是否有分号,Go 会自动的添加分号. 这些语句还是认为自己在不同的一行中
|
||||
>
|
||||
>
|
||||
|
||||
|
||||
## 为什么下面的代码可以运行
|
||||
```go
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(runtime.NumCPU() + 10)
|
||||
}
|
||||
```
|
||||
|
||||
1. 运算符可以组合表达式 *正确*
|
||||
2. 语句可以和表达式一起使用
|
||||
3. 表达式可以返回多值
|
||||
|
||||
> **1:** 是的, + 运算可以组合 `runtime.NumCPU()` 和 `10` 表达式
|
||||
>
|
||||
>
|
||||
> **2:** 不对,不可以组合使用。例如,不能这样做:`import "fmt" + 3`. 一些语句需要表达式,但这不意味着语句和表达式可以组合使用
|
||||
>
|
||||
>
|
||||
> **3:** 对的,但不能解释上面代码为什么可以运行
|
||||
>
|
||||
>
|
@ -0,0 +1 @@
|
||||
## 请查看 statements 目录下的问题
|
80
translation/chinese/04-语句-表达式-注释/问题/03-comments/README.md
Normal file
80
translation/chinese/04-语句-表达式-注释/问题/03-comments/README.md
Normal file
@ -0,0 +1,80 @@
|
||||
## 为什么有时需要用到注释?
|
||||
1. 为了将不同的表达式组合起来
|
||||
2. 为了给代码提供说明或者自动生成文档 *正确*
|
||||
3. 为了让代码看起来更漂亮
|
||||
|
||||
|
||||
## 下面的代码哪个是正确的?
|
||||
1.
|
||||
```go
|
||||
package main
|
||||
|
||||
/ main function is an entry point /
|
||||
func main() {
|
||||
fmt.Println("Hi")
|
||||
}
|
||||
```
|
||||
|
||||
2. *正确*
|
||||
```go
|
||||
package main
|
||||
|
||||
// main function is an entry point /*
|
||||
func main() {
|
||||
fmt.Println(/* this will print Hi! */ "Hi")
|
||||
}
|
||||
```
|
||||
|
||||
3.
|
||||
```go
|
||||
package main
|
||||
|
||||
/*
|
||||
main function is an entry point
|
||||
|
||||
It allows Go to find where to start executing an executable program.
|
||||
*/
|
||||
func main() {
|
||||
fmt.Println(// "this will print Hi!")
|
||||
}
|
||||
```
|
||||
|
||||
> **1:** `/` 不是注释,单行注释是 `//`.
|
||||
>
|
||||
>
|
||||
> **2:** 多行注释可以放在任何位置。但是,当有一个以 `/*`开头的注释,需要以 `*/` 结尾。 这里,Go 不关心 `/* ... */` 里面的内容, 它直接跳过。同时,Go 看到以 `//` 开头的代码,直接跳过整行代码。
|
||||
>
|
||||
>
|
||||
> **3:** `//` 阻止 Go 解析后面的内容,这就是它不能运行的原因, Go 不能解析 `"this will print Hi!")` 因为它们是注释。
|
||||
>
|
||||
>
|
||||
|
||||
## 应该如何命名代码,以便 Go 可以从代码自动生成文档?
|
||||
1. 通过注释的每一行代码,这样就会自动生成文档
|
||||
2. 通过在声明变量后开始进行注释 *正确*
|
||||
3. 通过使用多行注释
|
||||
|
||||
> **1:** 对不起,这没用
|
||||
>
|
||||
>
|
||||
> **3:** 它不关心使用单行注释还是多行注释
|
||||
>
|
||||
>
|
||||
|
||||
|
||||
## 在命令行打印文档需要用到下面哪个?
|
||||
1. go build
|
||||
2. go run
|
||||
3. go doctor
|
||||
4. go doc *正确*
|
||||
|
||||
|
||||
## `godoc` 和 `go doc` 有什么不同?
|
||||
1. `go doc` 是 `godoc` 背后真实的工具
|
||||
2. `godoc` 是 `go doc` 背后真实的工具 *正确*
|
||||
3. `go` 是 `go doc` 背后真实的工具
|
||||
4. `go` 是 `godoc` 背后真实的工具
|
||||
|
||||
> **2:** 是的, go doc 背后使用 godoc, go doc 只是简单版本的 godoc
|
||||
>
|
||||
>
|
Reference in New Issue
Block a user