-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGo Basics.txt
More file actions
173 lines (106 loc) · 3.33 KB
/
Go Basics.txt
File metadata and controls
173 lines (106 loc) · 3.33 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
Go Basics
Getting Started
--------------------
go mod init <module_name>
creates god mod file in project
go mod tidy
go get <package>
fetches a 3rd party package and downloads it to $GOPATH/pkg/mod
this MUST be done before "importing" the module and referencing in your project
go run main.go
runs the main.go file in the current directory
go build
compiles project into a binary
Variable Declaration
---------------------------
var myString string // intialize to zero value
myString = "Low"
x := "Hello World" // shorthand way to take the type or result on the right and assign it to the variable on the left
deck := []string{"2 of Hearts", "Jack of Diamonds"} // slice
fmt.Println("Hello World") // print to stdout
colors := map[string]string {
"red": "#ff0000",
"green": "#4bf765",
}
String Interpolation
------------------------
- the "f" stands for formatted string
name := "Low"
output := fmt.Sprintf(Hello my name is %a!, name)
Errorf("Expected deck length of 16, but got %d!", len(d))
Value & Reference Types
-----------------------------------
[ Value Types ]
int
float
string
bool structs
**Use pointers to change value types inside a function
[ Reference Types ]
slices
maps
channels
pointers
functions
**Don't need to worry about pointers for reference types; the language handles this
Pointers & Reference Syntax
--------------------------------------
When we call a function that takes an argument, that argument is copied to the function
func zero(x int) {
x = 0
}
func main() {
x := 5
zero(x)
fmt.Println(x) // x is still 5
}
In the instance above, I do not intend modify the original value of "x" in the main function
func zero(xPtr *int) {
*xPtr = 0
}
func main() {
x := 5
zero(&x)
fmt.Println(x) // x is 0
}
Pointers reference a location in memory where a value is stored rather than the value itself (they point to something else). By using a pointer (*int) the zero function is able to modify the original variable.
In Go a pointer is represented using the * (asterisk) character followed by the type of the stored value. In the zero function xPtr is a pointer to an int.
Finally we use the & operator to find the address of a variable. &x returns a *int (pointer to an int) because x is an int. This is what allows us to modify the original variable. &x in main and xPtr in zero refer to the same memory location.
Data Structures
--------------------
[ Arrays ]
- discuss the difference between this and slices
- common methods
[ Slices ]
[ Maps ]
- similar to objects (javascript), dicitonaries (python)
- key/value pairs
- keys must be of the same type; values must be of the same type
[ Structs ]
- talk about receivers
[ Interfaces ]
Functions
------------
Control Flow & Iteration
--------------------------------
func contains(vId string) ([]verse, bool) {
var tempSlice []verse
for _, item := range verses { // _ ignores the index value since we don't need it
if item.ID == vId {
tempSlice = append(tempSlice, item)
return tempSlice, true
}
}
return tempSlice, false
}
Error Handling Patterns
-------------------------------
Testing
---------
- files must have a _test in the name (e.g. deck_test.go)
- test are executed by running `go test`
Web Frameworks
------------------------
GIN
ORMs
---------