Update: packages-and-scopes Chinese version

This commit is contained in:
parkma99 2021-12-29 23:58:08 +08:00 committed by İnanç Gümüş
parent 7dd41dcf4b
commit e4c3980bec
25 changed files with 719 additions and 0 deletions

View File

@ -0,0 +1,15 @@
// 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 bye() {
fmt.Println("Bye!")
}

View File

@ -0,0 +1,15 @@
// 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 hey() {
fmt.Println("Hey!")
}

View File

@ -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
import "fmt"
func main() {
fmt.Println("Hello!")
// 你可以使用其他文件的函数
// 只要它们在同一个包中
// 因此, `main()` 可以调用 `bye()` and `hey()`
// 因为 bye.go, hey.go 和 main.go
// 都在 main package 中
bye()
hey()
}

View File

@ -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
// 文件作用域
import "fmt"
// 包作用域
const ok = true
// 包作用域
func main() { // 块作用域开始
var hello = "Hello"
// hello 和 ok 都可见
fmt.Println(hello, ok)
} // 块作用域结束

View 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
func nope() { //块作用域开始
// hello 和 ok 只定义在当前块作用域
const ok = true
var hello = "Hello"
_ = hello
} // 块作用域结束
func main() { // 块作用域开始
// hello 和 ok 不可见
// ERROR:
// fmt.Println(hello, ok)
} // 块作用域结束

View File

@ -0,0 +1,39 @@
// 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"
// 我不想在这节课中谈论这个话题
// 作为旁注,我想把它放在这里
// 记得复习
var declareMeAgain = 10
func nested() { // 块作用域开始
// 定义相同的变量
// 它们可以同时存在
// 这个只属于块作用域
// 包作用域的变量仍然可以使用
var declareMeAgain = 5
fmt.Println("inside nested:", declareMeAgain)
} // 块作用域结束
func main() { // 块作用域开始
fmt.Println("inside main:", declareMeAgain)
nested()
// 包作用域的 declareMeAgain 不受 nested 函数的影响
fmt.Println("inside main:", declareMeAgain)
} // 块作用域结束

View File

@ -0,0 +1,15 @@
// 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 bye() {
fmt.Println("Bye!")
}

View File

@ -0,0 +1,15 @@
// 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 hey() {
fmt.Println("Hey!")
}

View File

@ -0,0 +1,38 @@
// 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!")
// 两个文件属于同一个包
// 在这里调用 bye.go 的 `bye()`
bye()
}
// 练习: 删除下面函数的注释
// 分析错误信息
// func bye() {
// fmt.Println("Bye!")
// }
// ***** EXPLANATION *****
//
// ERROR: "bye" function "redeclared"
// in "this block"
//
// "this block" 是指 = "main package"
//
// "redeclared" 是指在同一个作用域中使用了相同的名字
//
// main package's 作用域是:
// main 包中的所有源代码文件

View File

@ -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
// 取消下面代码的注释
// (只删除最后三行的 //)
// 这个文件看不到 main.go's 导入的 ("fmt")的名字
// 因为倒入重命名是在文件作用域
// func bye() {
// fmt.Println("Bye!")
// }

View File

@ -0,0 +1,15 @@
// 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!")
}

View File

@ -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"
import f "fmt"
func main() {
fmt.Println("Hello!")
f.Println("There!")
}

View File

@ -0,0 +1,31 @@
// 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 文件,并在 main 函数里调用它们里面的函数
//
// 1- 创建 main.go, greet.go 和 bye.go 文件
// 2- 在 main.go 中: 调用 greet 和 bye 函数
// 3- 运行 `main.go`
//
// 提升
// greet 函数定义在 greet.go
// bye 定义在 bye.go
//
// 目标输出
// hi there
// goodbye
// ---------------------------------------------------------
func main() {
// 在这里调用其他文件的函数
}

View File

@ -0,0 +1,15 @@
// 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 bye() {
fmt.Println("goodbye")
}

View File

@ -0,0 +1,15 @@
// 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 greet() {
fmt.Println("hi there")
}

View File

@ -0,0 +1,14 @@
// 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() {
greet()
bye()
}

View File

@ -0,0 +1,32 @@
// 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. 创建两个文件: main.go 和 printer.go
//
// 2. 在 printer.go 中:
// 1. 创建一个叫 hello 的函数
// 2. hello 函数的作用是输出你的名字
//
// 3. 在 main.go 中:
// 1. 和之前一样,创建 main 函数
// 2. 调用你定义的函数,通过函数名 : hello
// 3. 创建一个叫 bye 的函数
// 4. 在 bye 函数中, 输出 "bye bye"
//
// 4. 在 printer.go 中:
// 1. 在 hello 函数中
// 调用 bye 函数
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,39 @@
// 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() {
// as you can see, I don't need to import a package
// and I can call `hello` function here.
//
// this is because, package-scoped names
// are shared in the same package
hello()
// but here, I can't access the fmt package without
// importing it.
//
// this is because, it's in the printer.go's file scope.
// it imports it.
// this main func can also call bye function here
// bye()
}
// printer.go can call this function
//
// this is because, bye function is in the package-scope
// of the main package now.
//
// main func can also call this.
func bye() {
fmt.Println("bye bye")
}

View File

@ -0,0 +1,20 @@
// 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 hello() {
// only this file can access the imported fmt package
// when others also do so, they can also access
// their own `fmt` "name"
fmt.Println("hi! this is inanc!")
bye()
}

View File

@ -0,0 +1,32 @@
// 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
// ---------------------------------------------------------
// 练习: imports 重命名
//
// 1- 使用不同的名字导入 fmt 包三次
//
// 2- 使用它们输出一些消息
//
// 目标输出
// hello
// hey
// hi
// ---------------------------------------------------------
// ?
// ?
// ?
func main() {
// ?
// ?
// ?
}

View File

@ -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"
import f "fmt"
import fm "fmt"
func main() {
fmt.Println("hello")
f.Println("hey")
fm.Println("hi")
}

View File

@ -0,0 +1,13 @@
1. **[使用自己定义的包](https://github.com/inancgumus/learngo/tree/master/03-packages-and-scopes/exercises/01-packages)**
创建几个 Go 文件,并在 main 函数里调用它们里面的函数
2. **[作用域的初体验](https://github.com/inancgumus/learngo/tree/master/03-packages-and-scopes/exercises/02-scopes)**
了解跨包作用域的效果
3. **[imports 重命名](https://github.com/inancgumus/learngo/tree/master/03-packages-and-scopes/exercises/03-importing)**
使用不同的名字导入同一个包

View File

@ -0,0 +1,50 @@
## 属于同一个包的源代码存储在哪里?
1. 每个文件应该存储在不同的目录
2. 在同一个目录下 *正确*
## 为什么 Go 源代码需要使用 package 语句?
1. 用来导入包
2. 用来让 Go 知道该文件属于哪个包 *正确*
3. 用来声明新函数
> **1:** `import` 语句的作用.
>
>
> **3:** `func` 语句的作用
>
>
## `package clause` 需要写在 Go 源文件的哪里?
1. 写在 Go 源文件的开头 *正确*
2. 写在 Go 源文件的末尾
3. 写哪里都可以
## 一个 Go 源文件使用 `package clause` 几次
1. 一次 *正确*
2. 零次
3. 多次
## 下面哪一个正确使用 `package clause`?
1. `my package`
2. `package main`
3. `pkg main`
## 哪一个说法是正确?
1. 在同一个包下的文件不能调用其他文件定义的函数
2. 在同一个包下的文件可以调用其他文件定义的函数 *正确*
## 如何运行多个 Go 源代码
1. go run *.*go
2. go build *go
3. go run go
4. go run *.go *正确*
> **4:** 也可以是 (假设目录下有 file1.go file2.go 和 file3.go): go run file1.go file2.go file3.go
>
>

View File

@ -0,0 +1,40 @@
## 下面哪一个是 Go 中正确的软件包类型?
* Empty package
* Executable package *正确*
* Transferrable package
* Librarian package
## 下面哪一个 package `go run` 可以执行?
* Empty package
* Executable package *正确*
* Transferrable package
* Library package
## 下面哪个 package `go build` 可以编译
* Empty package
* Temporary package
* Both of executable and library packages *正确*
* Transferrable package
## 下面哪一个是可执行的包
* 有 `func main``package main` *正确*
* 有 `func Main``package Main`
* 有 `func exec``package exec`
## 哪一个是 library package?
* `main package`
* `package lib` *正确*
* `func package`
* `package main``func main`
## 下面哪个包用于可执行 Go 程序?
* Empty package
* Executable package *正确*
* Transferrable package
* Library package
## 哪个包用于可重用并可以导入?
* Empty package
* Executable package
* Transferrable package
* Library package *正确*

View File

@ -0,0 +1,133 @@
## 作用域是什么?
* 可执行的代码块
* 声明变量的可见性 **正确**
* 确定要运行的内容
```go
package awesome
import "fmt"
var enabled bool
func block() {
var counter int
fmt.Println(counter)
}
```
## 哪个变量属于包作用域
1. awesome
2. fmt
3. enabled **正确**
4. counter
> **3:** 是的, `enabled` 在任何函数之外,所以它是包作用域的变量 `block()` 也是包作用域,它也在任何函数之外
>
>
## 哪一个变量属于文件作用域?
1. awesome
2. fmt **正确**
3. enabled
4. block()
5. counter
> **2:** 没错,导入的包名称属于文件作用域,而且它们只能在同一文件中使用。
>
>
## 哪一个在 block() 函数的作用域?
1. awesome
2. fmt
3. enabled
4. block()
5. counter **正确**
> **5:** 是的, `counter``block()` 函数内部定义,所以它属于函数作用域,对 `block()` 函数外的其他代码不可见
>
>
## `block()` 函数可以看见 `enabled` 变量吗?
1. 可以: 它在包作用域里 **正确**
2. 不可以: 它在文件作用域里
3. 不可以: 它在 block() 函数作用域里
> **1:** 包作用域的变量对同一个包下所有代码都可见
>
>
## `awesome`中的其他文件可以看见 `counter` 变量吗?
1. 可以
2. 不可以: 它在包作用域里
3. 不可以: 它在文件作用域里
4. 不可以: 它在 block() 函数的作用域里 **正确**
> **4:** 没错。其他代码都无法看到 `block()` 函数内部的变量。只有在 `block()` 函数内部的代码才能看到它们 (近在一定程度上 ,例如:函数内部的代码只能看到在它们之前声明的变量)
>
>
## `awesome` 中的其他文件可以看见 `fmt` 变量吗?
1. 可以
2. 不可以: 它在包作用域里
3. 不可以: 它在文件作用域里 **正确**
4. 不可以: 它在 block() 函数的作用域里
> **3:** 只有在 import 只定义在同一个文件内,不管是不是同一个包,其他文件都不可见
>
>
## 如果在相同的作用域中声明相同的变量会发生什么?
```go
package awesome
import "fmt"
// 在包作用域中声明两次
var enabled bool
var enabled bool
func block() {
var counter int
fmt.Println(counter)
}
```
1. 新声明的变量将覆盖前一个变量
2. 不能这样做,它已经在包作用域中声明了 *正确*
3. 不能这样做,它已经在文件作用域中声明了
> **2:** 没错,不能在同一个作用域内声明同一个变量,如果这样做,使用变量时获取的是前一个还是后一个声明的变量?
>
>
## 如果在不同的作用域中声明相同的变量会发生什么?
```go
package awesome
import "fmt"
// 在包作用域中声明
var enabled bool
func block() {
// 在函数作用域中声明
var enabled bool
var counter int
fmt.Println(counter)
}
```
1. 新声明的变量将覆盖前一个变量 *正确*
2. 不能这样做,它已经在包作用域中声明了
3. 不能这样做,它已经在文件作用域中声明了
> **1:** 实际上,可以在内部作用域中声明相同的变量,例如 `block()`'的作用域在包作用域里面。这意味这它可以获取到包作用域的变量(反之则不同). 所以, `block()`'作用域在包作用域之内。这意味着你可以再次声明相同的变量。它会覆盖掉父作用域的变量,它们同时存在,请查看课程存储库中的示例以找出答案。
>
>