File tree Expand file tree Collapse file tree 2 files changed +123
-0
lines changed
Expand file tree Collapse file tree 2 files changed +123
-0
lines changed Original file line number Diff line number Diff line change 1+ # method summary
2+
3+ ### Syntactic sugar:
4+
5+ #### Non-syntactic sugar
6+ ```
7+ type A struct {
8+ name string
9+ }
10+
11+ func (a A) Name() string {
12+ a.name = "a"
13+ return a.name
14+ }
15+
16+ func main() {
17+ a:= A{name:"aaa"}
18+
19+
20+ //Calling method:
21+ fmt.Println(a.Name())
22+
23+ //Value transfer, value copy (non-syntactic sugar)
24+ fmt.Println(A.Name(a))
25+
26+ }
27+ ```
28+
29+ #### Non-syntactic sugar, pointer receiver actual operation
30+ ```
31+ type A struct {
32+ name string
33+ }
34+
35+ func (a *A) Name() string {
36+ a.name = "a"
37+ return a.name
38+ }
39+
40+ func main() {
41+
42+ a:= &A{name:"aaa"}
43+
44+ //Calling method: the receiver of the corresponding operation
45+ fmt.Println(a.Name())
46+
47+ //Value transfer, pointer address copy (non-syntactic sugar)
48+ fmt.Println( (*A).Name(a))
49+
50+ }
51+ ```
52+
53+ #### Syntactic sugar:
54+
55+ ```
56+ a.Name() is converted to (*a).Name()
57+ ```
58+
59+
60+ ** Syntax sugar conversion occurs at compile time. **
Original file line number Diff line number Diff line change 1+ # method 小结
2+
3+ ### 语法糖:
4+
5+ #### 非语法糖
6+ ```
7+ type A struct {
8+ name string
9+ }
10+
11+ func (a A) Name() string {
12+ a.name = "a"
13+ return a.name
14+ }
15+
16+ func main() {
17+ a:= A{name:"aaa"}
18+
19+
20+ //调用方式:
21+ fmt.Println(a.Name())
22+
23+ //值传递,值拷贝(非语法糖)
24+ fmt.Println(A.Name(a))
25+
26+ }
27+ ```
28+
29+ #### 非语法糖 指针接收者实际操作
30+ ```
31+ type A struct {
32+ name string
33+ }
34+
35+ func (a *A) Name() string {
36+ a.name = "a"
37+ return a.name
38+ }
39+
40+ func main() {
41+
42+ a:= &A{name:"aaa"}
43+
44+ //调用方式:对应操作的接收者
45+ fmt.Println(a.Name())
46+
47+ //值传递,指针地址拷贝(非语法糖)
48+ fmt.Println( (*A).Name(a) )
49+
50+ }
51+ ```
52+
53+ #### 语法糖:
54+
55+ ```
56+ a.Name()转换为(*a).Name()
57+ ```
58+
59+
60+
61+ ** 语法糖转换是在编译期发生。**
62+
63+
You can’t perform that action at this time.
0 commit comments