go nuts with go and php
TRANSCRIPT
GO NUTS WITH GOand PHP
@mgiglesias
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
IT'S ALL ABOUTInteroperability
Other languages
PHP programmers
IT'S ALL ABOUTInteroperabilityAPIs
API centric design
User API
Payments API
Fraud API
Notifications API
PHP
Java
Python
Go
Emails API
REST
Ok. So GO.Why?
Google had issues. Yes it did.
Compilation / building time
Endless dependencies (#ifndef)
Concurrency
Garbage collection
Cross-compilation
Strict types
Performance (duh!)
Isn't it constantly changing?
$ go fix app.go
What can I use it for?
In WORKANA:URL shortener https://github.com/mariano/goshorty
Real time notification system
High performance tracking
Map Reduce
* 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)
Ol Mundo
package mainimport "fmt"func main() {fmt.Println("Ol Brasil!")
}
$ go fmt hello_world.go$ go build hello_world.go$ ./hello_world
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},}
Object oriented... really?
There are no concept of classes
No inheritance (duh!)
Everything is about composition
No classes?
const roleAdmin = "admin";
type People {Email stringRegistered time.Timerole string
}
func (p *People) IsAdmin() bool {
return (p.role == roleAdmin)
}
Public
Private
Sort of like $this
No classes?
type User interface {IsAdmin() bool
}
func printUser(u User) {fmt.Println("Is Admin:", u.IsAdmin())
}
u := &People{Email: "[email protected]",role: roleAdmin,
}printUser(u)
Instead of inheritance, composition
type Person interface {Name() string
}type User struct {PersonEmail string
}type RegisteredUser struct {PersonName 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()
Sort of like traits
Functions
func do(a int64, b int64) (x int64, y float64) {x = a * by = float64(a / b)return
}
func main() {g := func(n int64) (int64, float64) {return do(n, 10)
}fmt.Println(g(50))
}
Closure
Named return
Multiple values
Errors vs. Exceptions
func Connect(host string) (Connection, error) {// .
}
c, err := Connect("localhost")if err != nil {panic(err)
}
Defers
func log(m string) error {f, err := file.Open("/tmp/debug.log")if err != nil {return err
}defer f.Close()f.WriteString(m)
}
Goroutines
func main() {go func() {for {if !worker.Work() {break
}
}
}()fmt.Println("Waiting...")time.Sleep(5 * time.Second)
}
Not very nice. What can wedo to know goroutine is done?
* La rutina se ejecuta en un threadpool. Dependiendo del compilador (gccgo, etc) puede ser un thread por routine, o no
Channels
func main() {finished := make(chan bool)go func() {defer close(finished)for {if !worker.Work() {finished