1. Why do I like
Go?
Ikai Lan - @ikai
plus.ikailan.com
NYC GTUG
July 27, 2011
Wednesday, July 27, 2011
2. 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
3. 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
4. Fast, modern languages
• Standard library: JSON, websockets, web
server; more
• Garbage collection
• Multiple return values
• Unicode
Wednesday, July 27, 2011
5. 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
6. Flexible interfaces
// This is an interface declaration
type myInterface interface {
set(i int)
}
// This is a Type declaration. Note that it is a type, not a class
type 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
7. First class functions
package main
import "fmt"
// Make a function that returns a new function
func 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 %vn", makeAdd(100)(1));
fmt.Printf("value of makeAdd(200)(2) is %vn", makeAdd(200)(2));
}
// Outputs:
// value of makeAdd(100)(1) is 101
// value of makeAdd(200)(2) is 202
Wednesday, July 27, 2011
8. 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
9. Goroutines and
package main
Channels
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: %vn", i)
}
Wednesday, July 27, 2011
10. 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