Go nuts with Go and PHP

7,001 views

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,001
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
21
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide
  • * Map reduce: el master divide el problema en problemas mas chicos, se los da a workers. (Mapeo). Luego el worker devuelve el resultado, el master collecciona los resultados de sus workers, y genera su resultado (Reduce)
  • * La rutina se ejecuta en un threadpool. Dependiendo del compilador (gccgo, etc) puede ser un thread por routine, o no
  • Go nuts with Go and PHP

    1. 1. GO NUTS WITH  GO and PHP @mgiglesia
    2. 2. About me ● Love to be here! – ● ● Ouro Preto! Miramar, Argentina I code a lot (C++, Node.js, PHP, Python, Java,  Go, Ruby) ● PHP community ● Workana
    3. 3. IT'S ALL ABOUT Interoperability
    4. 4. Other languages
    5. 5. PHP programmers
    6. 6. IT'S ALL ABOUT Interoperability APIs
    7. 7. API centric design User API PHP Payments  Java API Fraud API Python Notifications  Go API Emails API REST
    8. 8. Ok. So GO. Why?
    9. 9. Google had issues. Yes it did. ● Compilation / building time ● Endless dependencies (#ifndef) ● Concurrency ● Garbage collection ● Cross­compilation ● Strict types ● Performance (duh!)
    10. 10. Isn't it constantly changing? $ go fix app.go
    11. 11. What can I use it for? ● In WORKANA: – URL shortener →  https://github.com/mariano/goshorty – Real time notification system – High performance tracking – Map Reduce
    12. 12. Olá Mundo package main import "fmt" func main() { fmt.Println("Olá Brasil!") } $ go fmt hello_world.go $ go build hello_world.go $ ./hello_world
    13. 13. Packages $ 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 }, }
    14. 14. Object oriented... really? ● There are no concept of classes ● No inheritance (duh!) ● Everything is about composition
    15. 15. No classes? const roleAdmin = "admin"; type People { Public Email string Registered time.Time role string } Private func (p *People) IsAdmin() bool { return (p.role == roleAdmin) } Sort of like  $this
    16. 16. No classes? type User interface { IsAdmin() bool } func printUser(u User) { fmt.Println("Is Admin:", u.IsAdmin()) } u := &People{ Email: "test@email.com", role: roleAdmin, } printUser(u)
    17. 17. Instead of inheritance, composition 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: "test@email.com", } r := &RegisteredUser{ name: "Mariano", } fmt.Println(u.Name(), r.Name() Sort of like traits
    18. 18. Functions Multiple values func do(a int64, b int64) (x int64, y float64) { x = a * b y = float64(a / b) return Named return } Closure func main() { g := func(n int64) (int64, float64) { return do(n, 10) } fmt.Println(g(50)) }
    19. 19. Errors vs. Exceptions func Connect(host string) (Connection, error) { // …. } c, err := Connect("localhost") if err != nil { panic(err) }
    20. 20. Defers func log(m string) error { f, err := file.Open("/tmp/debug.log") if err != nil { return err } defer f.Close() f.WriteString(m) }
    21. 21. Goroutines func main() { go func() { for { if !worker.Work() { break } } }() fmt.Println("Waiting...") time.Sleep(5 * time.Second) } Not very nice. What can we do to know goroutine is done
    22. 22. Channels func main() { finished := make(chan bool) go func() { defer close(finished) for { if !worker.Work() { finished <- true break } } }() <-finished }
    23. 23. Select in channels pings := make(chan int) go func() { i := 0 for { pings <- i i++ time.Sleep(time.Second) } }()
    24. 24. Select in channels messages := make(chan string) go func() { i := 0 for { messages <- fmt.Sprintf("Message #%d", i) i++ time.Sleep(time.Second * 3) } }()
    25. 25. Select in channels go func() { for { select { case m := <- messages: fmt.Println("[MESSAGE]", m) case p := <- pings: fmt.Println("[PING]", p) } } }() <- time.After(time.Second * 10)
    26. 26. Some Tips
    27. 27. Think memory and performance! m := make(map[string]map[string]string) a := make(map[string]string) a["key1"] = "value1" a["key2"] = "value2" m["settings"] = a a2 := &Settings{ key1: "value1", key2: "value2", } m2 := &Data{ settings: a2, }
    28. 28. Web framework? ● Gorilla – mux: router / dispatcher – sessions: diferentes stores (filesystem / cookies) – schema: from POST to struct ● martini ● web.go ● revel
    29. 29. Questions? @mgiglesia

    ×