Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go aka Golang

Go aka Golang

PHPDay Argentina 2013

Mariano Iglesias

April 27, 2013
Tweet

More Decks by Mariano Iglesias

Other Decks in Programming

Transcript

  1. Resolver problemas en Google • Tiempo de compilación / build

    • Lío de dependencias (#ifndef) • Concurrencia • Garbage collection • Cross­compilation • Tipos estrictos • Performance (duh!)
  2. Ok, pero ¿para qué lo puedo usar? • En WORKANA:

    – URL shortener → https://github.com/mariano/goshorty – Real time notification system – High performance tracking – Calculos Map Reduce
  3. El Hola Mundo package main import "fmt" func main() {

    fmt.Println("Hola PHPDay!") } $ go fmt hello_world.go $ go build hello_world.go $ ./hello_world
  4. Paquetes $ go get github.com/garyburd/redigo/redis import ( "github.com/garyburd/redigo/redis" "time" )

    redis.Pool{ MaxIdle: 5, IdleTimeout: 240 * time.Second, Dial: func () (redis.Conn, error) { c, err := redis.Dial("tcp", "localhost") if err != nil { return nil, err } return c, nil }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, }
  5. ¿No hay clases? const roleAdmin = "admin"; type People {

    Email string Registered time.Time role string } func (p *People) IsAdmin() bool { return (p.role == roleAdmin) } Públicos Privado Onda $this
  6. ¿No hay clases? type User interface { IsAdmin() bool }

    func printUser(u User) { fmt.Println("Is Admin:", u.IsAdmin()) } u := &People{ Email: "[email protected]", role: roleAdmin, } printUser(u)
  7. En vez de herencia... type Person interface { Name() string

    } type User struct { Person Email string } type RegisteredUser struct { Person Name string } func (u *User) Name() string { return u.Email } func (r *RegisteredUser) Name() string { return u.name } u := &User{ Email: "[email protected]", } r := &RegisteredUser{ name: "Mariano", } fmt.Println(u.Name(), r.Name() Una onda traits
  8. Funciones func do(a int64, b int64) (x int64, y float64)

    { x = a * b y = float64(a / b) return } func main() { g := func(n int64) (int64, float64) { return do(n, 10) } fmt.Println(g(50)) } Closure Named return Múltiples valores
  9. Errores vs. Exceptions func Connect(host string) (Connection, error) { //

    …. } c, err := Connect("localhost") if err != nil { panic(err) }
  10. Defers func log(m string) error { f, err := file.Open("/tmp/debug.log")

    if err != nil { return err } defer f.Close() f.WriteString(m) }