Created
January 22, 2019 08:43
-
-
Save miguelmota/835f8dc6935b98b9fbb77766511477ab to your computer and use it in GitHub Desktop.
Golang event emitter example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type MyEmitter map[string]chan string | |
func main() { | |
myEmitter := MyEmitter{} | |
myEmitter["my-event"] = make(chan string) | |
myEmitter["my-other-event"] = make(chan string) | |
go func() { | |
for { | |
select { | |
case msg := <-myEmitter["my-event"]: | |
fmt.Println(msg) | |
case msg := <-myEmitter["my-other-event"]: | |
fmt.Println(msg) | |
} | |
} | |
}() | |
myEmitter["my-event"] <- "hello world" | |
myEmitter["my-other-event"] <- "hello other world" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EventEmitter = require('events') | |
class MyEmitter extends EventEmitter {} | |
const myEmitter = new MyEmitter() | |
myEmitter.on('my-event', msg => { | |
console.log(msg) | |
}) | |
myEmitter.on('my-other-event', msg => { | |
console.log(msg) | |
}) | |
myEmitter.emit('my-event', 'hello world') | |
myEmitter.emit('my-other-event', 'hello other world') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment