1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<title>Go Tech Talk</title>
6
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
<meta name="font-size-adjustment" content="-1" />
8
<link rel="stylesheet" href="slidy.css"
9
type="text/css" media="screen, projection, print" />
10
<script src="slidy.js" type="text/javascript">
14
<!-- this defines the slide background -->
16
<div class="background">
19
<!-- sized and colored via CSS -->
22
<div class="footer"></div>
25
<div class="slide titlepage">
28
<img src="../go-logo-white.png" width="588px" height="217px">
30
<h1 style="padding-right: 0pt; margin-right: 0pt; color: #0066cc; font-size: 250%; border-bottom: 0px;">The Go Programming Language</h1>
31
<div style="color: #ffcc00;">
33
<h3>Sydney University<br/><br/>March 23, 2010</h3>
43
<h2>Garbage Collected</h2>
44
<h2>Systems Language</h2>
55
fmt.Printf("Hello, 世界\n")
61
<h1>Hello, world 2.0</h1>
63
<h2>Serving <a href="http://localhost:8080/world">http://localhost:8080/world</a></h2>
72
func handler(c *http.Conn, r *http.Request) {
73
fmt.Fprintf(c, "Hello, %s.", r.URL.Path[1:])
77
http.ListenAndServe(":8080",
78
http.HandlerFunc(handler))
86
<h2>It's about two years old:</h2>
88
<li>Design started in late 2007</li>
89
<li>Implementation starting to work mid-2008</li>
90
<li>Released as an open source project in November 2009</li>
91
<li>Development continues with an active community</li>
94
<h2>Why invent a new language? Older languages weren't designed for concurrency, but modern software needs it:</h2>
96
<li>Large scale, networked computing, such as Google web search</li>
97
<li>Multi-core hardware</li>
104
<h2>Older languages are also frustrating on a day-to-day basis</h2>
105
<h2>Statically-typed languages (C, C++, Java) have issues:</h2>
107
<li>Edit-Compile-Run cycle takes far too long</li>
108
<li>Type hierarchy can hurt as much as it helps</li>
110
<div style="text-align:center">
111
<img src="java-typing.png" width="800px" height="90px"><br>
114
<h2>Dynamic languages (Python, JavaScript) fix some issues but introduce others:</h2>
116
<li>No compilation means slow code</li>
117
<li>Runtime errors that should be caught statically</li>
120
<h2>Go has the lighter feel of a scripting language but is compiled</h2>
126
<h2>Large C++ programs (e.g. Firefox, OpenOffice, Chromium) have enormous build times:</h2>
128
<li>XKCD's #1 Programmer Excuse for Legitimately Slacking Off: "<a href="http://xkcd.com/303/">My Code's Compiling</a>"</li>
131
<h2>On a Mac (OS X 10.5.8, gcc 4.0.1):</h2>
133
<li>C: <code>#include <stdio.h></code> reads 360 lines from 9 files</li>
134
<li>C++: <code>#include <iostream></code> reads 25,326 lines from 131 files</li>
135
<li>Objective-C: <code>#include <Carbon/Carbon.h></code> reads 124,730 lines from 689 files</li>
136
<li>We haven't done any real work yet!</li>
139
<h2>In Go: <code>import "fmt"</code> reads <i>one</i> file: 184 lines summarizing 7 packages</h2>
145
<h2>Compilation demo</h2>
149
<h1>Experimental</h1>
151
<h2>Go is still unproven</h2>
152
<h2>Language is still evolving</h2>
153
<h2>Package library is incomplete</h2>
154
<h2>Concurrent garbage collection is an active research problem</h2>
155
<h2>Reviving forgotten concepts:</h2>
157
<li>Go's concurrency is strongly influenced by <i>Communicating Sequential Processes</i> (Hoare, 1978)</li>
158
<li>Go has types and interfaces, but no inheritance. It is arguably more object-oriented than previously mentioned languages, being closer to the original Smalltalk meaning (1970s)</li>
165
<h2>Unix philosophy: write <i>programs</i> that do one thing and do it well</h2>
166
<h2>Connect them with <i>pipes</i>:</h2>
168
<li>How many lines of test code are there in the Go standard library?</li>
169
<li><code>find ~/go/src/pkg | grep _test.go$ | xargs wc -l</code></li>
172
<h2>Unlike other languages, Go makes it easy to:</h2>
174
<li>Launch <i>goroutines</i></li>
175
<li>Connect them with <i>channels</i></li>
182
<h2>Start a new flow of control with the <code>go</code> keyword</h2>
183
<h2>Parallel computation is easy:</h2>
186
go expensiveComputation(x, y, z)
187
anotherExpensiveComputation(a, b, c)
191
<h2>Roughly speaking, a goroutine is like a thread, but lighter weight:</h2>
193
<li>Goroutines have segmented stacks, and typically smaller stacks</li>
194
<li>This requires compiler support. Goroutines can't just be a C++ library on top of a thread library</li>
201
<h2>Consider web servers ("the C10k problem"):</h2>
203
<li>"Thread per connection" approach is conceptually neat, but doesn't scale well in practice</li>
204
<li>What does scale well (event-driven callbacks, asynchronous APIs) are harder to understand, maintain, and debug</li>
205
<li>We think "goroutine per connection" can scale well, and is conceptually neat</li>
209
rw := socket.Accept()
210
conn := newConn(rw, handler)
219
<h2>Let's look again at our simple parallel computation:</h2>
222
go expensiveComputation(x, y, z)
223
anotherExpensiveComputation(a, b, c)
227
<h2>This story is incomplete:</h2>
229
<li>How do we know when the two computations are done?</li>
230
<li>What are their values?</li>
237
<h2>Goroutines communicate with other goroutines via channels</h2>
239
func computeAndSend(ch chan int, x, y, z int) {
240
ch <- expensiveComputation(x, y, z)
245
go computeAndSend(ch, x, y, z)
246
v2 := anotherExpensiveComputation(a, b, c)
257
<h2>In traditional concurrent programs, you <i>communicate by sharing memory</i>. In Go, you <i>share memory by communicating</i>:</h2>
259
<li>Communication (the <code><-</code> operator) is sharing and synchronization</li>
262
<h2>Threads and locks are concurrency primitives; CSP is a concurrency model:</h2>
264
<li>Analogy: "Go To Statement Considered Harmful" (Dijsktra, 1968)</li>
265
<li><code>goto</code> is a control flow primitive; structured programming (<code>if</code> statements, <code>for</code> loops, function calls) is a control flow model</li>
268
<h2>Learning CSP changes the way you think about concurrent programming:</h2>
270
<li>Every language has its grain. If your Go program uses mutexes, you're probably working against the grain</li>
275
<h1>Garbage Collected</h1>
277
<h2>Automatic memory management makes writing (and maintaining) programs easier</h2>
278
<h2>Especially in a concurrent world:</h2>
280
<li>Who "owns" a shared piece of memory, and is responsible for destroying it?</li>
283
<h2>Large C++ programs usually end up with semi-automatic memory management anyway, via "smart pointers"</h2>
284
<h2>Mixing the two models can be problematic:</h2>
286
<li>Browsers can leak memory easily; DOM elements are C++ objects, but JavaScript is garbage collected</li>
291
<h1>Garbage Collected</h1>
293
<h2>Go is also a safer language:</h2>
295
<li>Pointers but no pointer arithmetic</li>
296
<li>No dangling pointers</li>
297
<li>Variables are zero-initialized</li>
298
<li>Array access is bounds-checked</li>
301
<h2>No buffer overflow exploits</h2>
305
<h1>Systems Language</h1>
307
<h2>This just means you could write decently large programs in Go:</h2>
310
<li>Web browsers</li>
311
<li>Web crawlers</li>
312
<li>Search indexers</li>
314
<li>Word processors</li>
315
<li>Integrated Development Environments (IDEs)</li>
316
<li>Operating systems</li>
322
<h1>Systems Language</h1>
324
<h2>Garbage collection has a reputation for being "slower"</h2>
325
<h2>We're expecting Go to be slightly slower than optimized C, but faster than Java, depending on the task. Nonetheless:</h2>
327
<li>Fast and buggy is worse than almost-as-fast and correct</li>
328
<li>It is easier to optimize a correct program than to correct an optimized program</li>
329
<li>Fundamentally, it's simply a trade-off we're willing to make</li>
332
<h2>Memory layout can drastically affect performance. These two designs are equivalent in Go, but significantly different in Java:</h2>
334
type Point struct { X, Y int }
335
type Rect struct { P0, P1 Point }
339
type Rect struct { X0, Y0, X1, Y1 int }
344
<h1>Systems Language</h1>
346
<h2>Quote from http://loadcode.blogspot.com/2009/12/go-vs-java.html</h2>
349
"[Git] is known to be very fast. It is written in C. A Java version
350
JGit was made. It was considerably slower. Handling of memory and lack
351
of unsigned types was some of the important reasons.
354
<h2>Shawn O. Pearce wrote on the git mailinglist:</h2>
355
<ul><li>"JGit struggles with not
356
having an efficient way to represent a SHA-1. C can just say "unsigned
357
char[20]" and have it inline into the container's memory allocation. A
358
byte[20] in Java will cost an *additional* 16 bytes of memory, and be
359
slower to access because the bytes themselves are in a different area
360
of memory from the container object. We try to work around it by
361
converting from a byte[20] to 5 ints, but that costs us machine
366
Like C, Go does allow unsigned types and defining data structures
367
containing other data structures as continuous blocks of memory."
375
<h2>Experimental</h2>
377
<h2>Garbage Collected</h2>
378
<h2>Systems Language</h2>
382
<li>I haven't talked about the type system, interfaces, slices, closures, selects, ...</li>
383
<li>Documentation, mailing list, source code all online</li>
387
<div class="slide titlepage">
391
<img src="../gordon/bumper640x360.png" width="640px" height="360px">