-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass_point.go
73 lines (59 loc) · 1.58 KB
/
pass_point.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
package main
import "fmt"
func changeValues(quantity int, price float64, name string, sold bool) {
quantity = 3
price = 500.4
name = "Mobile Phone"
sold = false
}
func changeValuesByPointer(quantity *int, price *float64, name *string, sold *bool) {
*quantity = 3
*price = 500.4
*name = "Mobile Phone"
*sold = false
}
type Product struct {
price float64
productName string
}
func changeProduct(p Product) {
p.price = 300
p.productName = "Bicycle"
}
func changeProductByPointer(p *Product) {
(*p).price = 300
(*p).productName = "Bicycle"
}
func changeSlice(s []int) {
for i := range s {
s[i]++
}
}
func changeMap(m map[string]int) {
m["a"] = 10
m["b"] = 20
m["c"] = 30
}
func main() {
quantity, price, name, sold := 5, 300.4, "Laptop", true
fmt.Println("Before calling changeValue():", quantity, price, name, sold)
changeValues(quantity, price, name, sold)
fmt.Println("After calling changeValue():", quantity, price, name, sold)
changeValuesByPointer(&quantity, &price, &name, &sold)
fmt.Println("After calling changeValueByPointer():", quantity, price, name, sold)
gift := Product{
price: 100,
productName: "Watch",
}
changeProduct(gift)
fmt.Println(gift)
fmt.Println("Before calling the changeProductByPointer():", gift)
changeProductByPointer(&gift)
fmt.Println("After calling the changeProductByPointer():", gift)
prices := []int{1, 2, 3}
changeSlice(prices)
fmt.Println("prices slice after calling changeSlice():", prices)
myMap := map[string]int{"a": 1, "b": 2, "c": 3}
changeMap(myMap)
fmt.Println("myMap after calling changeMap():", myMap)
}