Beruflich Dokumente
Kultur Dokumente
Agenda
1. 2. 3. 4. 5. 6. 7. 8. Background - Why Go? A Simple Type System Organizing Go Code Concurrency Tools Libraries Where to go next? Q&A
Modern, general purpose programming language Compiles to native machine code Statically typed Lightweight syntax Type inference Simple type system Concurrency primitives
Background - Tenets of Go's design Simplicity Each feature should be easy to understand Orthogonality Gos features should interact in predictable and consistent ways Readability What is written on the page should be comprehensible with little context
pi := 3.142 // type float64 greeting := "Hello, NUS!" // type string pt := Point { 1.3667, 103.8 } //type Point mul := func(x, y int) int { return x * y } // type func(int, int) int
There are many Writer implementations in the standard library and other Go code. We have already seen an example:
func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello," + r.URL.Path[1:]) }
The fmt.Fprint function takes an io.Writer as its first argument and http.ResponseWriter implements the Write method (and hence the Writer interface). The fmt package does not know http.
Organizing Go Code
Packages
Go code is organized into packages A package is a collection of declarations: constants variables functions types Packages can span multiple files Code within a package can see all the top-level declarations within the package Related code can share internal details in a loose, informal way Easy to re-arrange and re-factor code No need to create a "grab bag" of utility classes
Packages - Visibility
Identifiers that begin with an upper case letter are "exported" and are therefore accessible from outside the package All other identifiers are "unexported' and private
package foo import "fmt" import "math" func InvalidPrintPi() { fmt.Println(math.pi) } func ValidPrintPi() { fmt.Println(math.Pi()) }
Concurrency
Concurrency
In UNIX we think about processes connected by pipes:
find ~/go/src/pkg | grep _test.go$ | xargs wc -l
Each tool is designed to do one thing and do it well. In Go: goroutines connected by channels Goroutines are like threads Share memory But cheaper: Smaller, segmented stacks. Many goroutines per OS thread. Channels are a typed conduit for: Synchronization Communication
Concurrency (cont)
The channel operator <- is used to send and receive values: func compute(ch chan int) { ch <- someComputation() } func main() { ch := make(chan int) go compute(ch) result := <-ch }
Concurrency - Synchronization
The channel operator <- is used to send and receive values: done := make(chan bool) doSort := func(s []int) { sort(s) done <- true } i := pivot(s) go doSort(s[:i]) go doSort(s[i:]) <- done <- done Unbuffered channel operations are synchronous; the send/recieve happens only when both sides are ready.
Concurrency - Communication
A common task: many workers are feeding from task pool. Traditionally, worker threads contend over a lock for work:
type Task struct { // some state } type Pool struct { Mu sync.Mutex Tasks []Task } func worker(pool *Pool) { for { pool.Mu.Lock() // acquire lock task := pool.Tasks[0] pool.Tasks = pool.Tasks[1:] pool.mu.Unlock() process(task) } }
Concurrency - Communication
A Go idiom: many worker goroutines receive tasks from a channel.
type Task struct { // some state } func worker(in, out chan*Task) { for { t := <-in process(t) out <- t } } func main() { pending, done := make(chan *Task) , make(chan *Task) go sendWork(pending) // send tasks to the pending channel for i := 0 ; i < 10 ; i++ { go worker(pending, done) } consumeWork(done) // receive tasks from the done channel
Tools
Tools
In Go 1, several existing tools have been consolidated into a single 'go' command to help you manage Go code. Some important ones we will be looking into: test - a simple test framework doc - used to extract Go documentation from source code build, install, get - build and package management run - compile and run There are some other commands available in the 'go' tool. Further information here: http://tip.golang.org/cmd/go/
Tools - go commands
test - A simple test framework
Write tests in Go Your tests use the code the same way the users will No need to learn a DSL Full power of Go is available Tests are built as part of the package Complex hooks not required
Tools go commands
doc - go doc [importpath...] Used to run the godoc command on source code to extract documentation. The godoc command has both command line and web interfaces. powers golang.org! Documentation written as plain comments No special format Not part of the language http://golang.org/cmd/godoc/ Tips on writing good Go Documentation can be found here: http://blog.golang.org/2011/03/godoc-documenting-go-code. html
Tools
build - go build [importpath...] Used to build go packages named by the paths provided install - go install [importpath...] Compiles and installs the packages specified in the importpaths, along with their dependencies. get - go get [importpath...] Downloads, compiles and installs the packages specified in the importpaths, along with their dependencies run - go run [gofiles...] Run compiles and runs the main package comprising the named source files.
Tools
fix - go fix [importpath...] Runs the fix command on the specified files fix - Go is a rapidly changing language, while Go 1 will introduce stability, the fix command allows the language to make non-backward compatible changes. Migrating between releases is usually just a matter of $ go fix /path/to/code
Libraries
Libraries Diverse, carefully-constructed, consistent standard library More than 150 packages Stable Also great external library support: http://godashboard.appspot.com/package MySQL, MongoDB, and SQLite3 database drivers, SDL bindings, Protocol Buffers, OAuth libraries, and much more.
App Engine!
Go runs on the App Engine Easiest way to get started with Go Announcement:
http://blog.golang.org/2011/05/go-and-google-app-engine.html
Guide:
http://code.google.com/appengine/docs/go/overview.html
Where to go next?
Open Source
Development began at Google in 2007 as a 20% project. Released under a BSD-style license in November 2009 Since its release, hundres of non-Google contributors have submitted over a thousand changes to the Go core. Full-time Go team at Google. All Go development ( code reviews etc) takes place in public. You are invited!
Try it!
Try an interactive tutorial on Go: http://tour.golang.org/ Watch some videos on Go: http://www.youtube. com/gocoding Visit our website and the official blog: http://golang.org http://blog.golang.org Find out further by browsing our documentation: http://golang.org/doc/docs.html
Questions?