talks / go-intro / go-intro.slide

Introduction To Go
24 July 2013
Tags: go

Miki Tebeka
Lead Software Developer, Demand Media

* Meta

- "If I had eight hours to chop down a tree, I'd spend six sharpening my axe." -Abraham Lincoln
- (A.K.A [[][Sharpen The Saw]])
- "If all you have is a hammer, everything looks like a nail." -Mark Twain(?)

Please join the [[!forum/sharper]["sharper" Google Group]].

Oh, and we're hosting [[][L.A. Gophers]] Thursday at 6pm (Hollywood room)
[Can I bribe you with some Pizza?]

* Go

The [[][official propaganda]] says:

The Go programming language is an open source project to make programmers more

Go is expressive, concise, clean, and efficient. Its concurrency mechanisms
make it easy to write programs that get the most out of multicore and networked
machines, while its novel type system enables flexible and modular program
construction. Go compiles quickly to machine code yet has the convenience of
garbage collection and the power of run-time reflection. It's a fast,
statically typed, compiled language that feels like a dynamically typed,
interpreted language.

* Why Go? (my take)

- Lightweight goroutines (avoid "callback hell")
- Channels (concurrency)
- Better OO
- Unicode
- Garbage Collector
- Standard library (and 3'rd party)
- Static (speed) yet feels dynamic (fast compilation, type inference)
- Tooling
- Community
- AppEngine

* Hello World

.play hw.go

* Hello World 2.0

.play hw2.go

See it at [[http://localhost:8080][http://localhost:8080]].

* Declaration

.code decl.go  /START_CONST/,/END_CONST/

.play decl.go  /START_MAIN/,/END_MAIN/

* Syntax

.code syn.go /START_FUNC/,/END_FUNC/

.play syn.go /START_FOR/,/END_FOR/

Other changes/oddities

- The default in `switch` is to `break`
- Switch cases don't have to be int or constants
- Function can return multiple values (more on this later)
- Unused imports/variables are compilation error (use _ to signal unused)

* range

.play range.go /START/,/END/

`range` works on channels as well.

* Defer and Errors

.code deferr.go /START_SLURP/,/END_SLURP/
.play deferr.go /START_MAIN/,/END_MAIN/

There's also [[][panic]] and [[][recover]] - I won't cover them here.

* Goroutines

- Lightweight execution unit (can span 10K+ on single core)
- Runtime will multiplex to threads/cores (see [[][GOMAXPROCS]])
- Expandable stack
- Shared memory

.play gr.go /START_GR/,/END_GR/

* A TCP Proxy

(taken from [[][seamless]])

.code fwd.go

* Channels

- Types pipes
- Preferred synchronization (though you can use [[]["regular" methods]])
- Blocks on read/write
- Can be buffered on not (default)

"Don't communicate by sharing memory; share memory by communicating."

* Example

.play chan.go /START/,/END/

* Select

.play select.go /START/,/END/

* Methods

.play point.go

* Interfaces

.code iface.go /START_IFACE/,/END_IFACE/
.play iface.go /START_MAIN/,/END_MAIN/

* Packages

- Files in same directory with same `package` directive
- Names starting with uppercase are exported

* A Peek At The Standard Library

[[][Main Page]]

Some notable packages

- [[][net/http]] - HTTP server and client
- [[][html/template]] - Templating
- [[][fmt]] - Formating (printf ++)
- [[][os]] - Open files ...
- [[][io]] - `io.Writer`, `io.Reader`
- [[][expvar]] - Metrics
- [[][flag]] - Command line parsing
- [[][database/sql]] - Database spec

* The "go" Tool

- `GOPATH` is your environment
- `build` will build your project
- `test` will test it (all `_test.go` files) - now with race detection
- `fmt` formats the code
- `get` will fetch packages (decentralized, however there's an [[][]])
- `run` will compile and run

* References

- [[][Main Site]]
- [[][Go Tour]] - Learn Go in your browser
- [[][Effective Go]]
- [[][How To Write Go Code]]
- [[][Package Documentation]]
- [[][3'rd Party Packages]]
- [[!forum/golang-nuts][Google Group]] (can also try [[][stackoverflow]])
- [[][Online Book]]

Tip: Use `golang` when googling

* Questions?

.image gopher.jpg