Created
March 21, 2018 16:01
-
-
Save aarti/71a2570d99062424a3386def98c2eb2e to your computer and use it in GitHub Desktop.
handlerFuncs and handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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