-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17-methods-2.go
More file actions
76 lines (54 loc) · 2.09 KB
/
17-methods-2.go
File metadata and controls
76 lines (54 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
)
type rectangle struct {
length int
width int
}
func area(r rectangle) {
fmt.Printf("Area Function result: %d\n", (r.length * r.width))
}
func (r rectangle) area() {
fmt.Printf("Area Method result: %d\n", (r.length * r.width))
}
func main() {
r := rectangle{
length: 10,
width: 5,
}
area(r)
r.area()
p := &r
/*
compilation error, cannot use p (type *rectangle) as type rectangle
in argument to area
*/
//area(p)
p.area()//calling value receiver with a pointer
}
/*
Value receivers in methods vs value arguments in functions
This topic trips most go newbies. I will try to make it as clear as possible 😀.
When a function has a value argument, it will accept only a value argument.
When a method has a value receiver, it will accept both pointer and value receivers.
Lets understand this by means of the example above.
--------------
function func area(r rectangle) in line no.12 accepts a value argument
and method func (r rectangle) area() accepts a value receiver.
In line no. 25, we call the area function with a value argument area(r) and it will work.
Similarly In line no. 26 we call the area method r.area() using a value receiver and this will work too.
We create a pointer p to r in line no. 28.
If we try to pass this pointer to the function area which accepts only a value,
the compiler will complain. I have commented line no. 33 which does this.
If you uncomment this line, then the compiler will throw error [compilation error,
cannot use p (type *rectangle) as type rectangle in argument to area]. This works as expected.
Now comes the tricky part, line no. 35 of the code p.area() calls the method area
which accepts only a value receiver using the pointer receiver p.
This is perfectly valid. The reason is that the line p.area(),
for convenience will be interpreted by Go as (*p).area() since area has a value receiver.
This program will output,
Area Function result: 50
Area Method result: 50
Area Method result: 50
*/