This is a creation in Article, where the information may have evolved or changed.
Events Source Code
Package Ax_eventsimport "FMT"//Event Type base class type event struct {//Event trigger Instance Target Ieventdispatcher//Event Type Strin G//Events carry data source Object interface{}}//Event Scheduler base class type Eventdispatcher struct {Savers []*eventsaver}//Event Scheduler Unit type Ev entsaver struct {type string Listeners []*eventlistener}//listener Type eventlistener struct {Handler Eventhand ler}//listener function Type EventHandler func (Event event)//Events Dispatch interface type Ieventdispatcher interface {//Event listener AddEventListener (EV Enttype string, listener *eventlistener)//Remove Event listener RemoveEventListener (EventType string, listener *eventlistener) Boo L//Whether it contains event Haseventlistener (EventType string) bool//Event dispatch dispatchevent bool}//Create event dispatcher func Newev Entdispatcher () *eventdispatcher {return new (Eventdispatcher)}//Create listener func neweventlistener (H EventHandler) *eventlis tener {L: = new (EventListener) L.handler = h return l}//Create event func newevent (EventType string, Object interface{}) Event {e: = Event{tyPe:eventtype, Object:object} return e}//Clone event func (this *event) clone () *event {e: = new (event) E.type = this. Type e.target = e.target return E}func (this *event) ToString () string {return FMT. Sprintf ("Event Type%v", this.) Type)}//Event Scheduler Add event func (this *eventdispatcher) AddEventListener (EventType string, listener *eventlistener) {for _, save R: = Range This.savers {if saver. Type = = EventType {Saver. Listeners = Append (saver. Listeners, Listener) return}} saver: = &eventsaver{type:eventtype, Listeners:[]*eventlistene R{listener}} this.savers = Append (this.savers, Saver)}//Event Scheduler removes a listener func (this *eventdispatcher) RemoveEventListener ( EventType string, listener *eventlistener) bool {for _, saver: = Range This.savers {if saver. Type = = EventType {For I, L: = Range saver. Listeners {if listener = = L {Saver. Listeners = Append (saver. LISTENERS[:I], saver. Listeners[i + 1:]...) Return True}}}} return false}//Event Scheduler contains a type of listener func (this *eventdispatcher) have EventListener (EventType string) bool {for _, saver: = Range This.savers {if saver. Type = = EventType {return true}} return false}//Event Scheduler Dispatch event func (this *eventdispatcher) Dispatche Vent (Event event) bool {for _, saver: = Range This.savers {if saver. Type = = event. Type {for _, Listener: = Range saver. Listeners {event. Target = This listener. Handler (Event)} return True}} return False}
Events contains several core classes:
Event
Eventdispatcher
EventListener
EventHandler
Eventsaver
Interface
Ieventdispatcher
Examples of use are as follows:
package mainimport ( "fmt" e "alex/ax_events" "time")const HELLO_WORLD = "helloWorld"func main() { dispatcher := e.NewEventDispatcher() listener := e.NewEventListener(myEventListener) dispatcher.AddEventListener(HELLO_WORLD, listener) time.Sleep(time.Second * 2) //dispatcher.RemoveEventListener(HELLO_WORLD, listener) dispatcher.DispatchEvent(e.NewEvent(HELLO_WORLD, nil))}func myEventListener(event e.Event) { fmt.Println(event.Type, event.Object, event.Target)}