2011 july-nyc-gtug-go

12
Ikai Lan - @ikai plus.ikailan.com NYC GTUG July 27, 2011 Why do I like Go? Wednesday, July 27, 2011

description

A quick talk about why I like Go.Note that the bit about Go on Google App Engine requiring

Transcript of 2011 july-nyc-gtug-go

Page 1: 2011 july-nyc-gtug-go

Ikai Lan - @ikaiplus.ikailan.com

NYC GTUGJuly 27, 2011

Why do I like Go?

Wednesday, July 27, 2011

Page 2: 2011 july-nyc-gtug-go

About the speaker

• Developer Relations at Google based out of San Francisco, CA

• I work on Google App Engine!

• Google+: plus.ikailan.com

• Twitter: @ikai

Wednesday, July 27, 2011

Page 3: 2011 july-nyc-gtug-go

Why Go?

• Fast, modern language

• Strongly typed, very flexible interfaces

• Functions as first class objects

• Ridiculously fast compilation times

• Concurrency baked in

• Tooling

Wednesday, July 27, 2011

Page 4: 2011 july-nyc-gtug-go

Fast, modern languages

• Standard library: JSON, websockets, web server; more

• Garbage collection

• Multiple return values

• Unicode

Wednesday, July 27, 2011

Page 5: 2011 july-nyc-gtug-go

Strongly typed

• ... but feels like a dynamic languages

• // Combo statement - We can infer type here, so no Java style type declaration redundancy

s := “This is a String”

Wednesday, July 27, 2011

Page 6: 2011 july-nyc-gtug-go

Flexible interfaces// This is an interface declarationtype myInterface interface { set(i int)}

// This is a Type declaration. Note that it is a type, not a classtype myType struct { i int }

// This is how we define a function where myType is a receiver// With an instance of myType we can call myType.set(123)func (p *myType) set(i int) { p.i = i }

// Because myType defines a function with the signature set(int i) method, // we can use it anywhere myInterface is accepted!func setToThousand(x myInterface) { myInterface.set(1000)}

Wednesday, July 27, 2011

Page 7: 2011 july-nyc-gtug-go

First class functionspackage main import "fmt"

// Make a function that returns a new functionfunc makeAdd(increment int) (counter func(int) int) { return func(v int) int { return v + increment; } }

func main() { fmt.Printf("value of makeAdd(100)(1) is %v\n", makeAdd(100)(1)); fmt.Printf("value of makeAdd(200)(2) is %v\n", makeAdd(200)(2)); }

// Outputs:// value of makeAdd(100)(1) is 101// value of makeAdd(200)(2) is 202

Wednesday, July 27, 2011

Page 8: 2011 july-nyc-gtug-go

Concurrency baked in

• goroutines - prefix a go in front of a method and it will run concurrently! Similar to appending & in *nix systems

• channels - blocking or buffered queues for cross process communication

Wednesday, July 27, 2011

Page 9: 2011 july-nyc-gtug-go

Goroutines and Channels

package main import ( "fmt" "time")

func doLotsOfWork(until int, ch chan int) { c := 0 for i := 0; i < until; i++ { c += i time.Sleep(1000) } ch <- c }

func main() { ch := make(chan int) // First the work off into the background go doLotsOfWork(5, ch) // Do more work here while we wait for this process to complete // Block until doLotsOfWork sends data back on this channel i := <- ch fmt.Printf("Final value: %v\n", i)}

Wednesday, July 27, 2011

Page 10: 2011 july-nyc-gtug-go

It runs on App Engine!

• Currently requires whitelisting

• Experimental status

• Goroutines allow for concurrency within request; concurrent requests coming

• Demo app: http://moustach-io.appspot.com/

• Source: https://code.google.com/p/appengine-go/source/browse/example/

Wednesday, July 27, 2011

Page 11: 2011 july-nyc-gtug-go

Mustachio

Wednesday, July 27, 2011

Page 12: 2011 july-nyc-gtug-go

Questions?

• Go (golang.org) - worth a look

• Google+: plus.ikailan.com

• Twitter: @ikai

Wednesday, July 27, 2011