Skip to content

Instantly share code, notes, and snippets.

@aarti aarti/main.go
Created Mar 21, 2018

Embed
What would you like to do?
handlerFuncs and handlers
package main
import "fmt"
func main() {
y := registrar{}
y.handlerFuncRegistry = map[string]handlerFunc{
"TheFuncHandler": implementation,
}
z := myHandler{}
y.handlerRegistry = map[string]handler{
"TheHandler": z,
}
y.invoker("TheFuncHandler", "Marie")
y.invoker("TheHandler", "Curie")
}
type registrar struct {
handlerFuncRegistry map[string]handlerFunc
handlerRegistry map[string]handler
}
func (r registrar) invoker(x string, data string) {
if _, ok := r.handlerFuncRegistry[x]; ok {
r.handlerFuncRegistry[x].implementation(data)
}
if _, ok := r.handlerRegistry[x]; ok {
r.handlerRegistry[x].implementation(data)
}
}
type handler interface {
implementation(string)
}
type handlerFunc func(string)
func (m handlerFunc) implementation(a string) {
m(a)
}
func implementation(a string) {
fmt.Println("HandlerFunc: Hello", a)
}
type myHandler struct{}
func (m myHandler) implementation(a string) {
fmt.Println("Handler: Hello", a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.