-
Notifications
You must be signed in to change notification settings - Fork 0
/
introduction_to_go.slide
293 lines (176 loc) · 5.67 KB
/
introduction_to_go.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
Introduction to Go
GDG Korea DevFair 2014
장재휴
Developer, Purpleworks
* Simple can be harder than complex
.image introduction_to_go/simple_can_be_harder_than_complex2.jpg 400 _
_단순함은_복잡함보다_어렵다_.
_생각을_명쾌하게_해_단순하게_만드려면_굉장히_노력해야_한다_.
_하지만_결국_그럴_가치가_있다_. _일단_단순함에_도달하면,_산을_움직일_수_있기_때문이다_.
* Agenda
- What is Go
- OOP in Go
- Concurrency in Go
- Real world Go
.image introduction_to_go/devfair2014_2.png _ 550
.image introduction_to_go/go-logo.jpg
* What is Go
* Why Go?
- Statically typed languages ➔ *Efficient* vs *Hard*to*write*
- Dynamic language ➔ *Easy*to*use* vs *Slow*
- *Speed* vs *Safety* vs *Ease*to*use*
- Concurrent programming is hard(thread, lock, mutex)
* What is go
- A modern, general purpose language.
- open source
- Statically typed languages
- Feel dynamically
- concurrents
- garbage-collected
- efficient
- simply
* OOP in Go
* 1. Object via struct and method
- No Classes. No "Objects"
- But Go is object-based
* Object in Go
.play introduction_to_go/object1.go
* Object in Go II
.play introduction_to_go/object2.go /START OMIT/,/END OMIT/
* 2. Code reuse
- No Inheritance
- *Composition* *over* *inheritance* principle
* Composition in Go
.play introduction_to_go/composition1.go /START OMIT/,/END OMIT/
* Composition in Go II
.play introduction_to_go/composition2.go /START OMIT/,/END OMIT/
* 3. Polymorphism via interface
- Interface is *just*set*of*methods*
- Interface define behavior (duck typing)
- "If something can do this, then it can be used here"
* Interfaces in Go
.code introduction_to_go/interface.go /START1 OMIT/,/END1 OMIT/ HLinterface
* Interfaces in Go II
.play introduction_to_go/interface.go /START OMIT/,/END OMIT/
* The Power of Interface
- Writer interface in standard *"io"* package
// http://godoc.org/io#Writer
type Writer interface {
Write(p []byte) (n int, err os.Error)
}
- Fprintln function in standard *"fmt"* package
func Fprintln(w io.Writer, a ...interface{}) (n int, err error)
* The Power of Interface
In handle function, just write to *io.Writer* object
func handle(w io.Writer, msg string) {
fmt.Fprintln(w, msg)
}
The *os.Stdout* can be used for *io.Writer*.
.play introduction_to_go/interface2.go /START OMIT/,/END OMIT/
* The Power of Interface
The *http.ResponseWriter* can be used for *io.Writer*.
.play introduction_to_go/interface3.go /START OMIT/,/END OMIT/
.link http://localhost:4000/hello-world localhost:4000/hello-world
.link http://localhost:4000/this-is-an-example-of-io.Writer localhost:4000/this-is-an-example-of-io.Writer
* Go is object-oriented!
- Go is object-based
- Go provides code reuse
- Polymorphism via interfaces
* Concurrency in Go
* What is concurrency
- *Composition* *of* *independently* *executing* *computations*.
- It is not parallelism.
* Go's Concurrency is
- Easy to understand.
- Easy to use.
- You don't need to be an expert!
* Go's approach
- In UNIX: processes connected by pipes:
find ~/go/src | grep _test.go$ | xargs wc -l
- In Go: *goroutines* connected by *channels*
* Fundamentals #1 - Goroutine
*Independently* *executing* *function*.
- The go statement launches a function call as a goroutine
go f()
go f(x, y, ...)
- It's not a thread
- Very lightweight
- A goroutine has its own stack
- A goroutine runs concurrently
* Goroutine example
.play introduction_to_go/goroutine.go /START OMIT/,/END OMIT/
* Fundamentals #2 - Channel-based communication
Channel allow goroutines to *exchange* *information* and *synchronize*.
- Define
chan int
- Create
ch = make(chan int)
- Use
ch <- 1 // send value 1 on channel ch
x = <-ch // receive a value from channel ch
* Communicating goroutines
.play introduction_to_go/channel.go /START OMIT/,/END OMIT/
* Communicating goroutines II
.play introduction_to_go/pingpong.go /START OMIT/,/END OMIT/
* Philosophy
- Goroutines give the efficiency of an *asynchronous*model*.
- But you can write code in a *synchronous*style*.
- _"Don’t_communicate_by_sharing_memory_._Instead,_share_memory_by_communicating."_
* Real world Go
* mixquare.com
- Microchats for everyone
- Instantly create a channel any topic, location or event
.image introduction_to_go/mixquare1.png _ 800
* Message Flow
.image introduction_to_go/mixquare_arch.png 500 _
* Action of message processing worker
1. save message to datastore
2. fetch channel information
3. fetch user information
4. publish
* Measure First
- ruby version
.image introduction_to_go/ruby.png
- go version
.image introduction_to_go/baseline.png
* Concurrency
- RPC-bound jobs are very common
- Excuting next job while waiting
* Concurrency II
- Run synchronously
err1 := msg.save()
c, err2 := msg.fetchChannel()
msg.setChannel(c)
u, err3 := msg.fetchUser()
msg.setUser(u)
if err1 != nil || err2 != nil || err3 != nil {
/* ... */
}
* Concurrency III
- Run concurrently
.code -numbers introduction_to_go/mixquare_concurrency.go
* Result(Concurrency)
- baseline
.image introduction_to_go/baseline.png
- concurrency
.image introduction_to_go/concurrency.png
* Caching
- Using SQL can be slow
- Using redis is good, but *fault* *tolerance* *is* *too* *hard*.
- *Solution*: Timeout waiting
* Caching with control variance
.code introduction_to_go/mixquare_fetch_user.go /START1 OMIT/,/END1 OMIT/
* Result(Caching)
- concurrency
.image introduction_to_go/concurrency.png
- caching
.image introduction_to_go/caching.png
* Before and After
- ruby version
.image introduction_to_go/ruby.png
- go version
.image introduction_to_go/caching.png
* Conclusion
* Go is... not so great
Go is young language