-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
127 lines (92 loc) · 2.91 KB
/
Copy pathtypes.go
File metadata and controls
127 lines (92 loc) · 2.91 KB
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
package main
import "fmt"
func main() {
// ============================ Strings and Chars ============================
// Strings are pretty straight-forward
myStr := "The Go Gopher looks creepy!"
// But you can also put them on multiple lines
myStr2 := `Finally, decent support for line
breaks in a string!`
// A single character, just like Java
myChar := 'c'
// That's right, emojis in code...
// (UTF-8 Source Code)
coffee := '☕'
fmt.Println(myStr)
fmt.Println(myStr2)
fmt.Println(myChar)
fmt.Println(coffee)
// ==========================================================================
// ============================ Floats ============================
// 64 Bit Float
pi := 22. / 7
// 32 Bit Float
var lessPrecisePi float32 = 22. / 7
fmt.Println(pi)
fmt.Println(lessPrecisePi)
// ===============================================================
// ============================ Arrays ============================
// Arrays have fixed size!
// The below would be equivalent to:
// {0, 0, 0, 0}
var arr [4]int
// You can also declare and initialize an
// array in one GO:
// The size is still fixed, automatically
// calculated as 5.
anotherArr := [...]int{1, 3, 2, 0, 100}
fmt.Println(arr)
fmt.Println(anotherArr)
// ===============================================================
// ============================ Slices ============================
// Slices are dynamically sized
slice := []int{1, 3, 5}
// Which means we can append to them
// (like ArrayLists in Java):
slice = append(slice, 7, 9) // {1, 3, 5, 7, 9}
// You can also initialize with default values:
zeroSlice := make([]int, 5) // {0, 0, 0, 0, 0}
// Arrays are copied so arrCopy and arr
// refer to different arrays!:
arrCopy := arr
// arrCopy: {0, 0, 0, 0}
// arr: {0, 0, 0, 0}
arrCopy[0] = 1
// arrCopy: {1, 0, 0, 0}
// arr: {0, 0, 0, 0}
// Slices refer to the slice so sliceCopy and
// zeroSlice refer to the same slice!:
sliceCopy := zeroSlice
// zeroSlice: {0, 0, 0, 0, 0}
// sliceCopy: {0, 0, 0, 0, 0}
sliceCopy[0] = 1
// zeroSlice: {1, 0, 0, 0, 0}
// sliceCopy: {1, 0, 0, 0, 0}
fmt.Println(slice)
fmt.Println(zeroSlice)
// ================================================================
// ============================ Maps ============================
// Maps are another useful data structure
ages := map[string]int{
"Imad": 22,
"Moh": 34,
"Val": 23,
}
// Happy Birthday!
ages["Imad"] = 23
// Maps also have reference semantics,
// meaning they work like slices when we
// "copy" them
// ==============================================================
// ============================ Pointers ============================
// Intialize a pointer
var p *int
// Set it to the address of some variable
myNum := 1
p = &myNum
// Dereference to read the value
fmt.Println(*p)
// Or to set the value
*p = 21
// ==================================================================
}