-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindirection.go
60 lines (47 loc) · 950 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
51
52
53
54
55
56
57
58
59
60
/*
Author: Resul Emre AYGAN
*/
package main
import (
"fmt"
"math"
)
type Vertex3 struct {
X, Y float64
}
func (v *Vertex3) Scale2(f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func ScaleFunc(v *Vertex3, f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func (v Vertex3) Abs2() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func AbsFunc(v Vertex3) float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func main() {
//Methods and pointer indirection
v := Vertex3{3, 4}
v.Scale2(2)
ScaleFunc(&v, 10)
p := &Vertex3{4, 3}
p.Scale2(3)
ScaleFunc(p, 8)
fmt.Println(v, p)
//Methods and pointer indirection (2)
q := Vertex3{3, 4}
fmt.Println(q.Abs2())
fmt.Println(AbsFunc(q))
w := &Vertex3{4, 3}
fmt.Println(w.Abs2())
fmt.Println(AbsFunc(*w))
//Choosing a value or pointer receiver
r := &Vertex3{3, 4}
fmt.Printf("Before scaling: %+v, Abs: %v\n", r, r.Abs2())
r.Scale2(5)
fmt.Printf("After scaling: %+v, Abs: %v\n", r, r.Abs2())
}