问题
我正在尝试将authboss ( https://github.com/volatiletech/authboss ) 与quicktemplate ( https://github.com/valyala/quicktemplate ) 一起使用。
authboss 渲染系统由一个接口定义:Renderer。
我已经创建了两个不同版本的解决方案,并且想知道哪一个是最好的,或者是否有第三个更好。
https://github.com/frederikhors/authbossQuicktemplate_1,带有工厂模式和初始化器
https://github.com/frederikhors/authbossQuicktemplate_2,带有接口和
SetData(data authboss.HTMLData) (page PageImpl)
方法
问题
我不知道要提高什么性能。
是否可以在硬件资源方面以不同的方式做同样的事情?
您认为我可以在某处使用指针进行改进吗?
相关代码
使用工厂模式和初始化器的解决方案:
type HTML struct { templates map[string]func(authboss.HTMLData) templates.PageImpl } func (h *HTML) Load(names ...string) error { for _, n := range names { switch n { case "login": h.templates[n] = InitializeLoginPage case "recover": h.templates[n] = InitializeRecoverPage } } return nil } func (h *HTML) Render(ctx context.Context, page string, data authboss.HTMLData) (output []byte, contentType string, err error) { buf := &bytes.Buffer{} tpl, ok := h.templates[page] if !ok { return nil, "", errors.Errorf("template for page %s not found", page) } templates.WritePage(buf, tpl(data)) return buf.Bytes(), "text/html", nil } func InitializeLoginPage(data authboss.HTMLData) templates.PageImpl { return &templates.LoginPage{Data: data} } func InitializeRecoverPage(data authboss.HTMLData) templates.PageImpl { return &templates.RecoverPage{Data: data} }
带有方法的接口的解决方案
type HTML struct { templates map[string]templates.AuthPageImpl } func (h *HTML) Load(names ...string) error { for _, n := range names { switch n { case "login": h.templates[n] = &templates.LoginPage{} case "recover": h.templates[n] = &templates.RecoverPage{} } } return nil } func (h *HTML) Render(ctx context.Context, page string, data authboss.HTMLData) (output []byte, contentType string, err error) { buf := &bytes.Buffer{} tpl, ok := h.templates[page] if !ok { return nil, "", errors.Errorf("template for page %s not found", page) } template := tpl.SetData(data) templates.WritePage(buf, template) return buf.Bytes(), "text/html", nil } type AuthPageImpl interface { SetData(data authboss.HTMLData) PageImpl } type LoginPage struct { Data authboss.HTMLData } type RecoverPage struct { Data authboss.HTMLData } func (p *LoginPage) SetData(data authboss.HTMLData) (page PageImpl) { p.Data = data return p } func (p *RecoverPage) SetData(data authboss.HTMLData) PageImpl { p.Data = data return p }