Sie sind auf Seite 1von 36

An Introduction to Go

Date: 10/2/2012 Kailash Sethuraman

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

Background - Why Go?


Statically typed languages are efficient, but bureaucratic and overly complex Dynamic languages can be easy to use, but are error prone, inefficient and can break down at scale Concurrent programming using the traditional threaded model can be very hard (threads, locks, headache). Speed, reliability or simplicity : pick two! Cant we do better?

Background - What is Go?


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

Background - Hello, world Hello, world:


package main import "fmt" func main() { fmt.Println("Hello, ") }

Background - Hello World 2


package main import ( "fmt" "http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello"+r.URL.Path[1:]) } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }

A Simple Type System

A Simple Type System


Java: Integer i = new Integer(1); C: int i = 1; Go: i := 1 Other languages with type inference that you may have heard of : Haskell, Scala, OCaml

A Simple Type System - Examples


i := 1 // type int

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

A Simple Type System - Types and Methods


You can define methods on any type: type Point struct { X, Y float64 } func (p Point) Abs() float64 { return math.Sqrt(p.X*p.X + p.Y*p.Y) } p := Point {4, 3} // type Point p.Abs() // == 5.0 , type float64

A Simple Type System - Types and Methods


You can define methods on any type: type MyFloat float64 func (m MyFloat) Abs() float64 { f := float64(m) // convert to a float64 if f < 0 { return -f } return f } f := MyFloat(-42) // a type conversion f.Abs() // == 42.0 , type float64 Go "objects" are just values, there is no "box".

A Simple Type System - Interfaces


Interfaces specify behaviours An interface type defines a set of methods:
type Abser interface { Abs() float64 }

A type that implements the methods, implicitly implements the interface:


func PrintAbs(a Abser) { fmt.Printf("Absolute value %.2f\n", a.Abs()) } PrintAbs(MyFloat(-10)) PrintAbs(Point{3, 4})

There is no "implements" declaration.

A Simple Type System - Interfaces in Practice


From the io package in the standard library:
type Writer interface { Write( p []byte) (n int, err error) }

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()) }

package math const pi = 3.142 func Pi() float64 { return 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

Eg: testing the io.Copy function:


func TestCopy(t *testing.T) { rb, wb := new(Buffer), new(Buffer) rb.WriteString("hello, world.") Copy(wb, rb) if wb.String() != "hello, world." { t.Errorf("Copy did not work properly") } }

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

Tips on writing scalable apps:


http://blog.golang.org/2011/11/writing-scalable-app-engine.html

Cant wait for Go 1 on the AppEngine?


https://groups.google.com/forum/#!topic/google-appengine-go/IlF2b1K-rXY

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?

Das könnte Ihnen auch gefallen