Skip to content

Commit 7ca3039

Browse files
committed
update lesson27
1 parent 2165cd1 commit 7ca3039

File tree

14 files changed

+144
-7
lines changed

14 files changed

+144
-7
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"package1/package2"
6+
"package1/package3"
7+
)
8+
9+
func init() {
10+
fmt.Println("test1")
11+
}
12+
13+
func init() {
14+
fmt.Println("test2")
15+
}
16+
17+
func main() {
18+
fmt.Println(package2.Add(1, 2))
19+
fmt.Println(package3.GetStr("abc"))
20+
fmt.Println(sub(1, 2))
21+
}
22+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func sub(a, b int) int {
6+
fmt.Printf("sub of %d and %d is below:\n", a, b)
7+
return a-b
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package package2
2+
3+
import "fmt"
4+
5+
func Add(a, b int) int {
6+
fmt.Println(multi(a, b))
7+
fmt.Printf("add of %d and %d is below:\n", a, b)
8+
return a+b
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package package2
2+
3+
import "fmt"
4+
5+
func multi(a, b int) int {
6+
fmt.Printf("multiply of %d and %d is below:\n", a, b)
7+
return a*b
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package package3
2+
3+
import "fmt"
4+
5+
func GetStr(str string) string {
6+
fmt.Printf("str of %s is below:\n", str)
7+
return str
8+
}

workspace/lesson27/gopath/readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Demo使用手册
2+
3+
1. 将package1这个文件夹拷贝到$GOPATH/src目录下
4+
5+
2. 在package1/main下,打开terminal
6+
7+
3. 在terminal里先设置关闭GO111MODULE
8+
9+
```sh
10+
export GO111MODULE=off
11+
```
12+
13+
4. 在terminal里执行如下编译和运行命令
14+
15+
```go
16+
go build -o main main.go util.go // 会生成一个名为main的可执行文件
17+
./main // 运行可执行文件
18+
```
19+
20+
21+
File renamed without changes.

workspace/lesson27/main.go renamed to workspace/lesson27/module/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"project/util/strings"
77
"sync"
88
)
9-
9+
func init() {
10+
fmt.Println("main init")
11+
}
1012
func main() {
1113
var result int
1214
var wg sync.WaitGroup

workspace/lesson27/module/readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Demo使用说明
2+
3+
1. 将module这个文件夹放在任意目录
4+
5+
2. 在module这个文件夹下,打开terminal
6+
7+
3. 在terminal里先确认GO111MODULE是否为on,如果不是的话使用下面的命令开启
8+
9+
```sh
10+
export GO111MODULE=on
11+
```
12+
13+
4. 在terminal里执行如下命令
14+
15+
```go
16+
go run main.go
17+
```
18+
19+
20+

workspace/lesson27/util/math.go renamed to workspace/lesson27/module/util/math.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func init() {
9-
fmt.Println("math")
9+
fmt.Println("math init")
1010
}
1111

1212
func Sum(a, b int) int {

workspace/lesson27/util/strings/reverse.go renamed to workspace/lesson27/module/util/strings/reverse.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package strings
22

3+
import "fmt"
4+
5+
func init() {
6+
fmt.Println("reverse init")
7+
}
8+
39
func Reverse(str string) string {
410
r := []rune(str)
511
reverseData := reverseRune(r)

workspace/lesson27/util/strings/reverse_help.go renamed to workspace/lesson27/module/util/strings/reverse_help.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package strings
22

3+
import "fmt"
4+
5+
func init() {
6+
fmt.Println("reverse_help init")
7+
}
8+
39
func reverseRune(r []rune) []rune {
410
size := len(r)
511
for i:=0; i<size/2; i++ {

workspace/lesson27/util/sub.go renamed to workspace/lesson27/module/util/sub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package util2
33
import "fmt"
44

55
func init() {
6-
fmt.Println("sub")
6+
fmt.Println("sub init")
77
}
88

99
func Sub(a, b int) int {

workspace/lesson27/readme.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,50 @@ Go 1.11开始,有了Go Modules,工程项目可以建在任何地方,代码
137137
### 不开启GO111MODULES时import package
138138

139139
1. 项目建在$GOPATH/src下面
140+
2. import package的时候路径从$GOPATH/src往下找
141+
142+
使用说明参考[gopath package demo](./gopath/)
140143

141-
2. import package的时候从$GOPATH/src
142144

143-
144145

145146
### 开启GO111MODULES时import package
146147

147-
1. 项目建在任何地方
148+
1. 项目可以建在任何地方
149+
148150
2. 在项目所在根目录创建go.mod文件
149-
3. import package的时候指定模块名称
151+
152+
```go
153+
go mod init module_name
154+
```
155+
156+
3. import项目里的package的时候指定go.mod文件里的模块名称
157+
158+
使用说明参考[module package demo](./module)
159+
160+
161+
162+
## init函数
163+
164+
init函数没有参数,没有返回值。
165+
166+
* 每个package里可以有多个init函数
167+
168+
* 每个源程序文件里也可以有多个init函数
169+
170+
* init函数不能被显示调用,在main()函数执行之前,自动被调用
171+
* 同一个pacakge里的init函数调用顺序不确定
172+
* 不同package的init函数,根据package import的依赖关系来决定调用顺序,比如package A里import了package B,那package B的init()函数就会比package A的init函数先调用。
150173

151174

152175

153176
## 注意事项
154177

155178
* package目录名和package目录下的Go源程序文件开头声明的包名可以不一样,不过一般还是写成一样,避免出错。
156179

180+
* 禁止循环导入package。
181+
182+
183+
157184
## References
158185

159186
* https://www.callicoder.com/golang-packages/

0 commit comments

Comments
 (0)