Skip to content

Commit 03bfaa1

Browse files
author
刘建秋
committed
update commit
1 parent 8d043af commit 03bfaa1

File tree

3 files changed

+239
-1
lines changed

3 files changed

+239
-1
lines changed

EN/defer.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# defer usage
2+
3+
### defer Default execution order
4+
5+
The defer method is an in - and out-stack structure.
6+
` ` `
7+
package main
8+
9+
import(
10+
"fmt"
11+
)
12+
13+
func main(){
14+
defer func(){
15+
fmt.Println("1")
16+
} ()
17+
defer func(){
18+
fmt.Println("2")
19+
} ()
20+
defer func(){
21+
fmt.Println("3")
22+
} ()
23+
24+
fmt.Println("4")
25+
fmt.Println("5")
26+
27+
return
28+
}
29+
30+
// output
31+
// 4
32+
// 5
33+
// 3
34+
// 2
35+
// 1
36+
` ` `
37+
38+
### defer Aborts execution
39+
40+
The defer stack operation after return will not be ignored
41+
` ` `
42+
package main
43+
44+
import(
45+
"fmt"
46+
)
47+
48+
func main(){
49+
defer func(){
50+
fmt.Println("1")
51+
} ()
52+
defer func(){
53+
fmt.Println("2")
54+
} ()
55+
56+
return
57+
58+
defer func(){
59+
fmt.Println("3")
60+
} ()
61+
defer func(){
62+
fmt.Println("4")
63+
} ()
64+
}
65+
66+
// output
67+
// 2
68+
// 1
69+
` ` `
70+
71+
### defer
72+
73+
** * Avoid using this type of operation as it is not recommended because it is not readable **
74+
* If the return value is not declared as a specific variable, defer will not change the return value variable
75+
76+
` ` `
77+
package main
78+
79+
import(
80+
"fmt"
81+
)
82+
83+
func main(){
84+
da := testA()
85+
fmt.Println(da)
86+
87+
db := testB()
88+
fmt.Println(db)
89+
}
90+
91+
// Function body: name return value
92+
func testA()(data string) {
93+
data = "OLD"
94+
defer func(){
95+
data = "NEW"
96+
} ()
97+
return res
98+
}
99+
100+
// Function body: non-name return value
101+
func testB()(string) {
102+
data := "OLD"
103+
defer func(){
104+
data = "NEW"
105+
} ()
106+
return res
107+
}
108+
109+
// output
110+
// NEW
111+
// OLD
112+
` ` `
113+
114+
# # # defer summary
115+
116+
* defer is used to **ensure** that a function call is performed later in a program's execution, usually for purposes of cleanup. defer is often used where e.g. ensure and finally would be used in other languages.
117+
118+
* defer in Go is like the C++ destructor; the Go area of C++ "destructors" is for functions, not objects.
119+
The * defer is the structure statement ** added when the function executes. It is the default, not the specified structure statement.

zh_CN/defer.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# defer 用法
2+
3+
### defer 默认执行顺序
4+
5+
defer方法是先进后出的栈入栈出结构。
6+
```
7+
package main
8+
9+
import(
10+
"fmt"
11+
)
12+
13+
func main(){
14+
defer func(){
15+
fmt.Println("1")
16+
}()
17+
defer func(){
18+
fmt.Println("2")
19+
}()
20+
defer func(){
21+
fmt.Println("3")
22+
}()
23+
24+
fmt.Println("4")
25+
fmt.Println("5")
26+
27+
return
28+
}
29+
30+
// output
31+
// 4
32+
// 5
33+
// 3
34+
// 2
35+
// 1
36+
```
37+
38+
### defer 中止执行
39+
40+
return之后的defer栈操作都不会被忽略
41+
```
42+
package main
43+
44+
import(
45+
"fmt"
46+
)
47+
48+
func main(){
49+
defer func(){
50+
fmt.Println("1")
51+
}()
52+
defer func(){
53+
fmt.Println("2")
54+
}()
55+
56+
return
57+
58+
defer func(){
59+
fmt.Println("3")
60+
}()
61+
defer func(){
62+
fmt.Println("4")
63+
}()
64+
}
65+
66+
// output
67+
// 2
68+
// 1
69+
```
70+
71+
### defer修改返回值注意事项
72+
73+
* **尽量避免使用此类操作,可读性不强不推荐**
74+
* 返回值如果是非声明具体变量,则defer不会修改返回值变量
75+
76+
```
77+
package main
78+
79+
import(
80+
"fmt"
81+
)
82+
83+
func main(){
84+
da := testA()
85+
fmt.Println(da)
86+
87+
db := testB()
88+
fmt.Println(db)
89+
}
90+
91+
// 函数体:命名式返回值
92+
func testA()(data string) {
93+
data = "OLD"
94+
defer func(){
95+
data = "NEW"
96+
}()
97+
return res
98+
}
99+
100+
// 函数体:非命名式返回值
101+
func testB()(string) {
102+
data := "OLD"
103+
defer func(){
104+
data = "NEW"
105+
}()
106+
return res
107+
}
108+
109+
// output
110+
// NEW
111+
// OLD
112+
```
113+
114+
### defer 小结
115+
116+
* defer用于确保函数调用在程序执行的后期执行,通常用于清理。Defer通常用于其他语言中使用的地方,例如ensure和finally。
117+
118+
* Go语言中defer类似于C++中的析构函数,Go语言区域于C++“析构“中针对的的是函数,而不是对象。
119+
* defer是函数执行时**添加**的结构语句,为缺省项,不是指定的结构语句。

zh_CN/map.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mp2 := make(map[int]*[]int)
1717
只遍历键KEY时,使用下面的形式,无须将值改为匿名变量形式,忽略值即可:
1818
```
1919
for key := range mapData {
20-
//Code ...
20+
// Code ...
2121
}
2222
```
2323

0 commit comments

Comments
 (0)