In this example we are going to have two types of  event-driven programming examples with Golang. They are very similar to each other but the main difference between both is the channel usage. The first example doesn’t use channels. You are never meant to touch the internal/pkg/event folder so you can consider it as an external library package. The user folder is your code.

This is an opinionated yet very straight forward “even driven” example. Events are dispatched in a certain way however the way they are handled is up to you. Dispatching events can be handled in a blocking or non-blocking manner (as goroutine). It has three simple steps as listed below.

  1. Create an instance of the event dispatcher. (done at application bootstrap)

  2. Register events and listeners. (done at application bootstrap)

  3. Dispatch events.

Properties

  • One dispatcher can register many listeners.

  • One dispatcher can register many events for a specific given listener.

  • One dispatcher can dispatch many events.

  • One listener can listen on many events.

  • One event can be linked to one event type.

  • The dispatcher prevents registering duplicated events.

  • The dispatcher prevents dispatching non-registered events.

#go

 Event Listener and Dispatcher Example with Golang
11.05 GEEK