I want to create a funcMap from all methods of a particular struct.
I have the following code:
type templateFuncs struct{}
func (t templateFuncs) UnescapeHTML(html string) template.HTML {
html = strings.ReplaceAll(html, " ", "")
html = strings.ReplaceAll(html, "nn", "n")
return template.HTML(html)
}
func (h *Handler) templateFuncs() template.FuncMap {
funcMap := make(template.FuncMap)
tplFuncs := &templateFuncs{}
rType := reflect.TypeOf(tplFuncs)
for i := 0; i < rType.NumMethod(); i++ {
method := rType.Method(i)
funcMap[method.Name] = method.Func.Interface()
fmt.Printf("method %#v", method)
}
return funcMap
}
However, this assigns a function where the first argument is a pointer receiver.
"UnescapeHTML":(func(*http.templateFuncs, string) template.HTML)(0x10333eb40)
Is there a way to assign the original function with just the string argument to the funcMap? Is there a better way for approaching this problem?