-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpointers & methods.go
103 lines (96 loc) · 2.55 KB
/
pointers & methods.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import "fmt"
type Myvar struct {
x, y float64
}
func (v1 *Myvar) func1(f float64) {
v1.x = v1.x * f
v1.y = v1.y * f
}
func (v2 *Myvar) func2(f float64) (float64, float64) {
return v2.x * f, v2.y * f
}
func func3(v3 *Myvar, f float64) {
v3.x = v3.x * f
v3.y = v3.y * f
}
func func4(v4 *Myvar, f float64) (float64, float64) {
return v4.x * f, v4.y * f
}
func (v5 Myvar) func5(f float64) {
v5.x = v5.x * f
v5.y = v5.y * f
}
func (v6 Myvar) func6(f float64) (float64, float64) {
return v6.x * f, v6.y * f
}
func func7(v7 Myvar, f float64) {
v7.x = v7.x * f
v7.y = v7.y * f
}
func func8(v8 Myvar, f float64) (float64, float64) {
return v8.x * f, v8.y * f
}
func main() {
v := Myvar{4, 8}
fmt.Println("---------------------------------")
v.func1(2)
// &v.func1(2) //can't use &v
fmt.Println(v.x, v.y)
fmt.Println("")
fmt.Println(v.func2(2))
// fmt.Println(&v.func2(2)) // can't use &v
fmt.Println("---------------------------------")
// func3(v, 2) //can't use v
func3(&v, 2)
fmt.Println(v.x, v.y)
fmt.Println("")
// fmt.Println(func4(v, 2)) // can't use v
fmt.Println(func4(&v, 2))
fmt.Println("---------------------------------")
// &v.func5(2) //can't use &v
v.func5(2)
fmt.Println(v.x, v.y)
fmt.Println("")
// fmt.Println(func6(&v, 2)) //can't use &v
fmt.Println(v.func6(2))
fmt.Println("---------------------------------")
func7(v, 2)
fmt.Println(v.x, v.y)
fmt.Println("")
fmt.Println(func8(v, 2))
fmt.Println("---------------------------------")
p := &Myvar{4, 8}
p.func1(2)
fmt.Println(p.x, p.y)
fmt.Println("")
fmt.Println(v.x, v.y) //p.x, p.y and v.x, v.y are different
fmt.Println("")
fmt.Println(p.func2(2))
fmt.Println("---------------------------------")
func3(p, 2)
fmt.Println(p.x, p.y)
fmt.Println("")
fmt.Println(v.x, v.y) //p.x, p.y and v.x, v.y are different
fmt.Println("")
fmt.Println(func4(p, 2))
fmt.Println("---------------------------------")
p.func5(2)
fmt.Println(p.x, p.y)
fmt.Println("")
v.func5(2) // even v.func() call will work
fmt.Println(v.x, v.y) //p.x, p.y and v.x, v.y are different
fmt.Println("")
fmt.Println(p.func6(2))
fmt.Println("---------------------------------")
// func7(p, 2) // can't use p because of &Myvar instead we can use *p
func7(*p, 2)
fmt.Println(p.x, p.y)
fmt.Println("")
func7(v, 2) // even func(v, something) call will work
fmt.Println(v.x, v.y) //p.x, p.y and v.x, v.y are different
fmt.Println("")
// fmt.Println(func8(p, 2)) // can't use p because of &Myvar instead we can use *p
fmt.Println(func8(*p, 2))
fmt.Println("---------------------------------")
}