-
Notifications
You must be signed in to change notification settings - Fork 0
/
class one.slide
207 lines (144 loc) · 5.45 KB
/
class one.slide
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
Let's Go
Class #1
Andrew Lader
* So, Let's Go!
First, has _everyone_ installed Go?
*Agenda*
- Why Go?
- Workspace
- Basics of Go
- Quick overview of pointers vs by-value
- Coding example
- Homework
.image ./images/goat_eating_homework.gif _ 128
* Why go with Go?
.image ./images/pros.png
- Compiles to machine code (read: *Fast*!!)
- Platform agnostic
- Garbage collected
- Concurrency is first class citizen (*channels* are our friend)
- Built-in unit testing
- Documentation is part of the language
- Rich standard library
- Building executables are easy and faster
.image ./images/gopher.png _ 64
* Why not Go?
.image ./images/cons.png
- A relatively young language, though not so much an issue anymore (we're on v1.10.1 and it's 6 yrs old)
- Does not have true OO
- Handling errors can be tedious, but required (maybe not a bad thing?)
* Workspace
Understanding a Go Workspace
- The `$GOPATH` environment points to the root folder for all of your Go projects
- Under that directory will be a `src` folder
- A project consists of the SCM `/` your name or organization `/` and the project name
github.com/your-org/repo-name
my-main-class.go // The main package, which is run when the service starts up
|
|-- 📂 webapi
|
|-- handlers.go // Contains all of the API handlers for the web service
|-- listener.go // The listener for all of the incoming HTTP requests, acts as the main entry point for the start of the service
|-- router.go // Routs the HTTP requests to a specific handler
|-- 📂 service
|
|-- dispatcher.go // Dispatches new work to an available worker from its pool of workers
|-- return.go // Lists the possible return values for the service
|-- service.go // The entry point for all API requests from the web
* Basics of Go
- Organized into packages
- A package name must match the folder name
- Each file in a package contains the package name, and has the extension `.go`
- Variables, structs and functions that have the first letter capitalized are exported
var Foo int
var boo string
- Executables have exactly one package called `main`
package main
func main() {
// kick off some work
// wait for it to finish
// exit
}
* Data Types
- The usual suspects:
bool
int, int8, int16, int32, int64
uint, uint8, uint16, uint32, uint64, uintptr
byte // (unit8)
rune // (int32)
float32, float64
complex64, complex128
string
- Variable declarations begin with the variable name followed by the type:
variableName string
anotherVariable int
trueOrFalse bool
* Data Structs & Functions
- Code predominantly consists of data structs and functions
- A struct is declared like so:
type foo struct {
firstName string
lastName string
addressOne string
amount float32
refCount int
}
- A function is declared like so, and can have multiple return values
func fooBar(inputOne string, inputTwo []byte, randomFlag bool) (*foo, error) {
// code goes here
return foo, nil
}
* Methods
- For most OO languages, a class definition includes methods
- in Go, which has limited OO, you have *receivers*
func (service *Service) CalculateTotalOrderAmount() float32 {
}
- Therefore, only a pointer to a struct called `Service` can call the function `CalculateTotalOrderAmount`
service := &Service{
orderLines []orderLine
}
total := service.CalculateTotalOrderAmount()
- Note the way the variables are assinged using `:=` as opposed to just `=`. This saves the step of declaring the variable first
* Error Handling
- Go has the concept of exceptions in the form of *panics*
- Panics should be used rarely, at best!
- Instead, functions should return errors, like so
output, err := json.Marshal(myObject)
if err != nil {
log.Printf("an error occurred marshing the object: %v", err)
return errors.New("This method failed because object marshaling failed")
}
* First coding example
- A simple `main` function
func main() {
var verbose bool
fmt.Print("**********************\n")
fmt.Print("Welcome to an example!\n")
fmt.Print("**********************\n\n")
flag.BoolVar(&verbose, "verbose", false, "verbose flag")
flag.Parse()
fmt.Printf("The verbose flag was set to: %v\n", verbose)
}
* Pass By-Reference vs By-value
- Pointers are faster
- With pointers, values can be modified within functions
- By-Value should be considered carefully for efficiency
[[https://play.golang.org/p/VhQ3fMu-7v][playground example]]
* Quicksort walkthrough
So, what is Quicksort?
Now, it's time to Code!
.image ./images/code.png _ 300
[[https://github.com/AndrewLader/go-sort/archive/master.zip][download qsort code]]
* Assignment due next week
- Create a simple Go application that
1. models a deck of cards (52 cards, 4 suits)
2. prints out the cards in the deck
3. shuffles the deck and prints out the new deck order
*** _I_will_be_avaialble_to_help_during_the_day_and_evening_hours_
* Resources
Go Basics from golang.org
[[https://golang.org/doc/code.html][How to Write Go Code]]
Workspaces
[[https://astaxie.gitbooks.io/build-web-application-with-golang/en/01.2.html][$GOPATH and Workspace]]
Playground
[[https://play.golang.org][Golang Playground]]