Source

talks / go-intro / go-intro.slide

Introduction To Go
24 July 2013
Tags: go

Miki Tebeka
Lead Software Developer, Demand Media
miki.tebeka@demandmedia.com
@tebeka

* Meta

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

Please join the [[https://groups.google.com/a/groups.demandmedia.com/forum/#!forum/sharper]["sharper" Google Group]].

Oh, and we're hosting [[http://www.meetup.com/Los-Angeles-Gophers/events/127123632/][L.A. Gophers]] Thursday at 6pm (Hollywood room)
[Can I bribe you with some Pizza?]


* Go

The [[http://golang.org/doc/][official propaganda]] says:

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

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 [[http://golang.org/doc/effective_go.html#panic][panic]] and [[http://golang.org/doc/effective_go.html#recover][recover]] - I won't cover them here.

* Goroutines

- Lightweight execution unit (can span 10K+ on single core)
- Runtime will multiplex to threads/cores (see [[http://golang.org/pkg/runtime/#GOMAXPROCS][GOMAXPROCS]])
- Expandable stack
- Shared memory

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

* A TCP Proxy

(taken from [[https://bitbucket.org/tebeka/seamless][seamless]])

.code fwd.go


* Channels

- Types pipes
- Preferred synchronization (though you can use [[http://golang.org/pkg/sync/]["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

[[http://golang.org/pkg/][Main Page]]

Some notable packages

- [[http://golang.org/pkg/net/http/][net/http]] - HTTP server and client
- [[http://golang.org/pkg/html/template/][html/template]] - Templating
- [[http://golang.org/pkg/fmt/][fmt]] - Formating (printf ++)
- [[http://golang.org/pkg/os/][os]] - Open files ...
- [[http://golang.org/pkg/io/][io]] - `io.Writer`, `io.Reader`
- [[http://golang.org/pkg/expvar/][expvar]] - Metrics
- [[http://golang.org/pkg/flag/][flag]] - Command line parsing
- [[http://golang.org/pkg/database/sql/][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 [[http://godoc.org/][http://godoc.org/]])
- `run` will compile and run


* References

- [[http://golang.org/][Main Site]]
- [[http://tour.golang.org][Go Tour]] - Learn Go in your browser
- [[http://golang.org/doc/effective_go.html][Effective Go]]
- [[http://golang.org/doc/code.html][How To Write Go Code]]
- [[http://golang.org/pkg/][Package Documentation]]
- [[http://godoc.org/][3'rd Party Packages]]
- [[https://groups.google.com/forum/#!forum/golang-nuts][Google Group]] (can also try [[http://stackoverflow.com/questions/tagged/go][stackoverflow]])
- [[http://www.golang-book.com/][Online Book]]

Tip: Use `golang` when googling

* Questions?

.image gopher.jpg