-
Notifications
You must be signed in to change notification settings - Fork 0
/
Go_Pointers.go
55 lines (40 loc) · 1.27 KB
/
Go_Pointers.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
//Every stand-alone go program must have a main package
package main
//fmt package - to print out statements
import "fmt"
//Call by Value method will affect only the dummy variables used within the function
func Go_Pointers_swap_func_CallByValue(m1 int, m2 int ) {
var temp int
temp = m1
m1 = m2
m2 = temp
}
//Call by function method will affect both dummy and actual variables
func Go_Pointers_swap_func_CallByReference(m1 *int, m2 *int ) {
var temp int
temp = *m1
*m1 = *m2
*m2 = temp
}
//Entry point for the program
func main() {
m1 := 2
m2 := 3
fmt.Println("Data of m1 =", m1)
fmt.Println("Data of m2 =", m2)
//'&' -> holds address of the variable
fmt.Println("Addr of m1 =", &m1)
fmt.Println("Addr of m2 =", &m2)
fmt.Println("After performing Call By Value")
//During Call by value, data of variables are sent as parameters
Go_Pointers_swap_func_CallByValue(m1, m2)
fmt.Println("After Swapping")
fmt.Println("m1 =", m1)
fmt.Println("m2 =", m2)
fmt.Println("After performing Call By Reference")
//During Call by reference, addresses of variables are sent as parameters
Go_Pointers_swap_func_CallByReference(&m1, &m2)
fmt.Println("After Swapping")
fmt.Println("m1 =", m1)
fmt.Println("m2 =", m2)
}