Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Something like this (if I understood your question correctly):
package main
import (
"fmt"
)
type HandlersMap map[string]func(string) string
func (hm HandlersMap) RegisterHandler(path string, handler func(string) string) {
hm[path] = handler
}
func main() {
m := make(HandlersMap, 0)
m.RegisterHandler("1", myFunc1)
m.RegisterHandler("2", myFunc2)
handler1 := m["1"]
handler2 := m["2"]
fmt.Println(handler1("handler 1 output"))
fmt.Println(handler2("handler 2 output"))
}
func myFunc1(s string) string {
return fmt.Sprintf("MyFunc1: %s", s)
}
func myFunc2(s string) string {
return fmt.Sprintf("MyFunc2: %s", s)
}
$ go run main.go
MyFunc1: handler 1 output
MyFunc2: handler 2 output
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question