• Stars
    star
    53
  • Rank 533,606 (Top 11 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The implementation of the pattern observer

Event

Build Status codecov Go Report Card GoDoc

This is package implements pattern-observer

Fast example

import (
	"github.com/agoalofalife/event"
)

func main() {
	// create struct
	e := event.New()

	// subscriber 
	e.Add("push.email", func(text string){
    	// some logic 
    }, "text")
    
    // init event
    e.Fire("push.email") // or e.Go("push.email")
}

let us consider an example:

  • You must first create the structure
  • Next, the first argument declares the name of the event (string type), second argument executes when the event occurs, the third argument is passed a list of arguments, which are substituted in the parameters of the second argument.
  • In the end you need to run the event. There are two methods available "Go" and his alias "Fire"

The subscriber function method

type Email struct {
	count int
}
func (e *Email) Push() {
	e.count += 1
	fmt.Printf("Push email again, count %d \n", e.count)
}
func main() {
	e := event.New()
	
	email := new(Email)
	
	e.Add(email, email.Push)
	e.Fire(email)
	e.Fire(email)
}
// output 
// Push email again, count 1 
// Push email again, count 2 

Bench

// create struct and add new event handler
BenchmarkAdd-8           1000000              1482 ns/op

// create struct and add new event handler and N run this handler
BenchmarkGo-8            5000000               339 ns/op

  • In this example we sign the event method structure

Read more information in examples 👍