-
Notifications
You must be signed in to change notification settings - Fork 0
/
17-methods-3.go
67 lines (48 loc) · 1.87 KB
/
17-methods-3.go
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
package main
import (
"fmt"
)
type rectangle struct {
length int
width int
}
func perimeter(r *rectangle) {
fmt.Println("perimeter function output:", 2*(r.length+r.width))
}
func (r *rectangle) perimeter() {
fmt.Println("perimeter method output:", 2*(r.length+r.width))
}
func main() {
r := rectangle{
length: 10,
width: 5,
}
p := &r //pointer to r
perimeter(p)
p.perimeter()
/*
cannot use r (type rectangle) as type *rectangle in argument to perimeter
*/
//perimeter(r)
r.perimeter()//calling pointer receiver with a value
}
/*
Pointer receivers in methods vs pointer arguments in functions.
Similar to value arguments, functions with pointer arguments will accept
only pointers whereas methods with pointer receivers will accept both value and pointer receiver.
Line no. 12 of the above program defines a function perimeter which accepts a pointer argument
and line no. 17 defines a method which has a pointer receiver.
In line no. 27 we call the perimeter function with a pointer argument and in line no.28
we call the perimeter method on a pointer receiver. All is good.
In the commented line no.33, we try to call the perimeter function with a value argument r.
This is not allowed since a function with pointer argument will not accept value arguments.
If this line is uncommented and the program is run, the compilation will fail
with error main.go:33: cannot use r (type rectangle) as type *rectangle in argument to perimeter.
In line no.35 we call the pointer receiver method perimeter with a value receiver r.
This is allowed and the line of code
r.perimeter() will be interpreted by the language as (&r).perimeter() for convenience.
This program will output,
perimeter function output: 30
perimeter method output: 30
perimeter method output: 30
*/