-
Notifications
You must be signed in to change notification settings - Fork 0
/
indirection.go
50 lines (36 loc) · 943 Bytes
/
indirection.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
/*
Function with a pointer argument must take a pointer
func ScaleFunc(v *Vertex, f float64) {}
var v Vertex
ScaleFunc(v, 5) // Compile error
ScaleFunc(&v, 5) // OK
While methods with pointer recievers take either a value or a pointer as the reciever when they are called
func (v *Vertex) Scale(f float64) {}
var v Vertex
v.Scale(5) //OK
p :=&v
p.Scale(5) //OK
For v.Scale(5), even though v is not a pointer, the method with the pointer reciever is called automaticlly.
GO interprets the statement v.Scale(5) as (&v).Scale(5) since the Scale method has a pointer reciever
*/
package main
import "fmt"
type Vertex struct {
X, Y float64
}
func ScaleFunc(v *Vertex, f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func (v *Vertex) Scale(f float64) {
v.X = v.X * f
v.Y = v.Y * f
fmt.Println("In Scale fn, vertex is, ", v)
}
func main() {
v := Vertex{3, 4}
p := Vertex{3, 4}
ScaleFunc(&v, 10)
p.Scale(10)
fmt.Println(v, p)
}