Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created January 22, 2019 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miguelmota/835f8dc6935b98b9fbb77766511477ab to your computer and use it in GitHub Desktop.
Save miguelmota/835f8dc6935b98b9fbb77766511477ab to your computer and use it in GitHub Desktop.
Golang event emitter example
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"
}
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