EventEmitter in Go

package main
import (
    "fmt"
    "github.com/chuckpreslar/emission"
)

func fibo(n int) (result int) {
    x, y := 0, 1
    for i := 0; i < n; i++ {
        result = x
        x, y = y, x+y
    }
    return result
}

func fibo1(arg int) {
    c := fibo(arg)
    fmt.Println("Fibo result", c)
}

func main() {
    emitter := emission.NewEmitter()
    emitter.On("fibo1", fibo1)
    emitter.Emit("fibo1", 21)
    emitter.Emit("fibo1", 14)
    emitter.Emit("fibo1", 7)
    emitter.Off("fibo1", fibo1) //zero receiver
    emitter.Emit("fibo1", 7)
    fmt.Scanln()
}
 
Output:

Fibo result 6765

Fibo result 233

Fibo result 8

Advertisements

Subjectivity aside, leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s