How to elegantly implement the job website?

Good afternoon. I need to implement the mission (for example to pardon two players) I could write a manual check right in the function of a pardon but then will have to be very tight with the addition of new missions and the editing of old (all code spread). I want to do it all in one place. Came to mind crazy idea 1: all processors send colback in the function handler missions
July 9th 19 at 13:56
3 answers
July 9th 19 at 13:58
Solution
As an option.
If you are using some framework, maybe there is a system of events, if not, then write your own. Further, every successful action causes the trigger to run its global event. And all the logic is already possible to process in a separate component, subscribing to desired events.
I must say, there's a problem with passing arguments, especially need context, but frameworks usually solve this problem.
Standard library. Only hardcore)) - Reta76 commented on July 9th 19 at 14:01
Well, very simple event. I will call in each function, and the processing of all events in one place. Do not think strong bad code
package events

var Event map[string]func(ps []string)

func init() {
 Event = make(map[string]func(ps []string))
}

// Subscribe to event
func Add(name string, callback func(ps []string)) {
 Event[name] = callback
}

// Call event
func Call(name string ps []string) {
Event[name](ps)
}
- Reta76 commented on July 9th 19 at 14:04
That this is not thread-safe. https://github.com/chuckpreslar/emission here is an extended version of your creation - ephraim_Schumm commented on July 9th 19 at 14:07
: that would make potatopak it is necessary to add blocking? I just have so much code on this principle made - Reta76 commented on July 9th 19 at 14:10
yeah, or mutexes or channels, but of course the question is "need it or not?". If you have a signature on all the events in init() methods, then it is not necessary, but if dynamically (i.e., in any method if the web service), then thread safety is required. - ephraim_Schumm commented on July 9th 19 at 14:13
: ie to block necessary when recording and when reading are not. Lock to hold something like this
maxDataLock.Lock()
 Event[name] = callback
 maxDataLock.Unlock()

Right? - Reta76 commented on July 9th 19 at 14:16
: When reading also need a lock. Take Libu I told you, she's not big (only 1 file) =) - ephraim_Schumm commented on July 9th 19 at 14:19
I myself want to understand how it works. I just just realized that I have a whole bunch potoko is not safe code. And the last question. If I created a global variable sync.Mutex it is possible then to use all the functions or for each rebuild? - Reta76 commented on July 9th 19 at 14:22
July 9th 19 at 14:00
Solution
package events

import (
"sync"
)

var Event map[string]func(ps []string)

func init() {
 Event = make(map[string]func(ps []string))
}

// Subscribe to event
func Add(name string, callback func(ps []string)) {
 rw := &sync.RWMutex{}
rw.Lock()
 defer rw.Unlock()

 Event[name] = callback

}

// Call event
func Call(name string ps []string) {
 rw := &sync.RWMutex{}
rw.RLock()
 defer rw.RUnlock()
 go Event[name](ps)

}
Are you sure that this is correct? The bottom line is this, you each time you call Add() and Call() make a new variable to lock, so if parallel and competitive to call the same function twice, there will be two separate Loka and the resulting Event variable is not protected in any way.

Below the code is sent as a separate response. - Reta76 commented on July 9th 19 at 14:03
July 9th 19 at 14:02
https://play.golang.org/p/NJ-X38UrWC

In addition to the comment on another answer.
package events

import (
"sync"
)

var events *Events

type Events struct {
sync.RWMutex
 list map[string]func(ps []string)
}

NewEventsList func() (list *Events) {
 list = new(Events)
 list.list = make(map[string]func(ps []string))
 return list
}

func init() {
 events = NewEventsList()
}

func Add(name string, callback func(ps []string)) {
 events.Add(name, callback)
}

func Call(name string ps []string) bool {
 return events.Call(name, ps)
}


// Subscribe to event
func (l *Events) Add(name string, callback func(ps []string)) {
l.Lock()
 defer l.Unlock()
 l.list[name] = callback
}

// Call event
func (l *Events) Call(name string ps []string) bool {
l.RLock()
 defer l.RUnlock()
 if f, ok := l.list[name]; ok {
 go f(ps)
 return true
}
 return false
}

Find more questions by tags GoWeb Development