2011 july-nyc-gtug-go
-
Upload
ikailan -
Category
Technology
-
view
914 -
download
0
description
Transcript of 2011 july-nyc-gtug-go
Ikai Lan - @ikaiplus.ikailan.com
NYC GTUGJuly 27, 2011
Why do I like Go?
Wednesday, July 27, 2011
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
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
Fast, modern languages
• Standard library: JSON, websockets, web server; more
• Garbage collection
• Multiple return values
• Unicode
Wednesday, July 27, 2011
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
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
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
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
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
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
Mustachio
Wednesday, July 27, 2011
Questions?
• Go (golang.org) - worth a look
• Google+: plus.ikailan.com
• Twitter: @ikai
Wednesday, July 27, 2011