-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
133 lines (98 loc) · 3.65 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
)
// Function that does not modify the original value
func zeroVal(val int) {
val = 0
}
// Function that modifies the value through a pointer
func zeroPtr(ptr *int) {
*ptr = 0
}
// Define a struct
type Person struct {
Name string
Age int
}
// Function that modifies the struct via a pointer
func updateAge(p *Person, newAge int) {
p.Age = newAge
}
// Function that appends a value to a slice using a pointer
func appendValue(slice *[]int, value int) {
*slice = append(*slice, value)
}
// Function that adds a key-value pair to a map using a pointer
func addEntry(m *map[string]int, key string, value int) {
(*m)[key] = value
}
func main() {
fmt.Println("-----------------------------------------------------------------------------------")
// Example with value
x := 5
fmt.Println("Before zeroVal:", x) // Output: Before zeroVal: 5
zeroVal(x)
fmt.Println("After zeroVal:", x) // Output: After zeroVal: 5
// Example with pointer
y := 5
fmt.Println("Before zeroPtr:", y) // Output: Before zeroPtr: 5
zeroPtr(&y)
fmt.Println("After zeroPtr:", y) // Output: After zeroPtr: 0
fmt.Println("-----------------------------------------------------------------------------------")
// Create an instance of Person
person := Person{Name: "Alice", Age: 30}
// Print the original Person
fmt.Println("Original person:", person) // Output: {Alice 30}
// Update age using a pointer
updateAge(&person, 35)
// Print the updated Person
fmt.Println("Updated person:", person) // Output: {Alice 35}
fmt.Println("-----------------------------------------------------------------------------------")
// Declare a slice
numbers := []int{1, 2, 3}
// Print the original slice
fmt.Println("Original slice:", numbers) // Output: [1 2 3]
// Append a value using a pointer
appendValue(&numbers, 4)
// Print the updated slice
fmt.Println("Updated slice:", numbers) // Output: [1 2 3 4]
fmt.Println("-----------------------------------------------------------------------------------")
// Declare a map
data := make(map[string]int)
// Print the original map
fmt.Println("Original map:", data) // Output: map[]
// Add entries using a pointer
addEntry(&data, "a", 1)
addEntry(&data, "b", 2)
// Print the updated map
fmt.Println("Updated map:", data) // Output: map[a:1 b:2]
fmt.Println("-----------------------------------------------------------------------------------")
// Declare an integer variable
a := 10
// Declare a pointer to the variable
p := &a
// Declare a pointer to the pointer
pp := &p
// Print values and addresses
fmt.Println("Value of x:", a) // Output: 10
fmt.Println("Value pointed to by p:", *p) // Output: 10
fmt.Println("Value pointed to by pp:", **pp) // Output: 10
fmt.Println("Address of p:", p) // Output: Address of x
fmt.Println("Address of pp:", pp) // Output: Address of p
// Assign a new value to the variable through the pointer-to-pointer
**pp = 20
// Print values after assignment through pp
fmt.Println("\nAfter assigning a new value through pp:")
fmt.Println("Value of a:", a) // Output: 20
fmt.Println("Value pointed to by p:", *p) // Output: 20
fmt.Println("Value pointed to by pp:", **pp) // Output: 20
// Directly assign a new value to p
*p = 30
// Print values after direct assignment to p
fmt.Println("\nAfter directly assigning a new value to p:")
fmt.Println("Value of a:", a) // Output: 30
fmt.Println("Value pointed to by p:", *p) // Output: 30
fmt.Println("Value pointed to by pp:", **pp) // Output: 30
fmt.Println("-----------------------------------------------------------------------------------")
}