+ -
Notes for current slide
Notes for next slide

.center

Go

static duck typing

and more...

Alexander Solovyov, PyGrunn 2012

Go

  • Announced on November 2009
    • Go 1 was released on 28 March 2012
  • Ken Thompson, Rob Pike
    • C, Unix, Plan 9, etc
  • Conservative, pragmatic
2 / 18

Conservative

  • { braces }
  • imperative
  • static typing
  • "unhurried" innovativity
3 / 18

Pragmatic

  • fast compilation
  • garbage collection
  • convenient type system
  • concurrency primitives
  • extensive standard library
  • UTF-8
4 / 18

Typing system

type Printable interface {
String() string
}
func Print(x Printable) {
println(x.String())
}
5 / 18

Typing system (error)

type Page struct {
Text string
}
func main() {
page := &Page{"test"}
// Type error
Print(page)
}
6 / 18

Typing system (fixed)

type Page struct {
Text string
}
func (page *Page) String() string {
return page.Text
}
func main() {
page := &Page{"test"}
Print(page)
}
7 / 18

Concurrency

func fib(n int) int {
// ...
return result
}
func main() {
ch := make(chan int)
go func() {
ch <-fib(5)
}
println(<- ch)
}
8 / 18

More!

func main() {
ch := make(chan int)
for i := 1; i <= 10; i++ {
go func(n int) {
ch <-fib(n)
}(i)
}
for i := 1; i <= 10; i++ {
println(<-ch)
}
}
9 / 18

Handling errors

func main() {
f, err := os.Open("path")
if err != nil {
log.Fatal(err)
}
defer f.Close()
...
}
10 / 18

HTTP daemon, Python

.python
class Responder(web.RequestHandler):
def get(self):
name = self.get_argument('id')
try:
with file(os.path.join(DIR, name)) as f:
content = f.read()
data = {"content": content}
except IOError:
data = {"error": "data not found"}
self.write(json.dumps(data))
app = web.Application([(r'/', Responder)])
app.listen(8080, 'localhost')
ioloop.IOLoop.instance().start()
11 / 18

HTTP daemon, Go

.small
func getContent(name string) map[string]string {
data, err := ioutil.ReadFile(filepath.Join(DIR, name))
if err == nil {
return map[string]string{"content": string(data)}
}
return map[string]string{"error": "data not found"}
}
func Responder(w http.ResponseWriter, req *http.Request) {
name := req.FormValue("id")
data, _ := json.Marshal(getContent(name))
w.Write(data)
}
func main() {
http.HandleFunc("/", Responder)
http.ListenAndServe(":8081", nil)
}
12 / 18

Unscientific benchmark

httperf, 100 clients, 1000 requests each

  • Python: 1900 req/s, 11 MB
  • PyPy: 3600 req/s, 70 MB
  • Go: 6100 req/s, 4 MB
13 / 18

Imports

import (
"fmt"
"os"
"github.com/droundy/goopt"
)

Supports Github, Bitbucket, Google Code, Launchpad

14 / 18

"go" tool

  • go build
  • go install
  • go get
  • go fmt
  • etc
15 / 18

Cross-compilation

.shell
$ GOOS=windows GOARCH=amd64 go build $PKG
16 / 18

Problems

  • Leaks on 32-bit systems
  • GC far from being perfect
  • Hard to search for
    • Use 'go lang' as search query
17 / 18

Questions?

.center Gopher

http://golang.org/

18 / 18

Go

  • Announced on November 2009
    • Go 1 was released on 28 March 2012
  • Ken Thompson, Rob Pike
    • C, Unix, Plan 9, etc
  • Conservative, pragmatic
2 / 18
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
s Start & Stop the presentation timer
t Reset the presentation timer
?, h Toggle this help
Esc Back to slideshow