dtf54486 2016-01-09 03:55
浏览 96
已采纳

go'Template.Execute'如何读取其匿名结构的参数字段?

In coding with Go, with html.template package, template.Execute is called.
I have a question, how could it read the field of anonymous structure of its parameter.
I read the source code, but it doesn't make sense. I don't have an idea.

/usr/local/go/src/html/template/tempalte.go L.78

type Template struct {
    ...
    text *template.Template
    ...
}

// escape escapes all associated templates.
func (t *Template) escape() error {
    t.nameSpace.mu.Lock()
    defer t.nameSpace.mu.Unlock()
    if t.escapeErr == nil {
        if t.Tree == nil {
            return fmt.Errorf("template: %q is an incomplete or empty template%s", t.Name(), t.text.DefinedTemplates())
        }
        if err := escapeTemplate(t, t.text.Root, t.Name()); err != nil {
            return err 
        }
    } else if t.escapeErr != escapeOK {
        return t.escapeErr
    }   
    return nil 
}

// Execute applies a parsed template to the specified data object,
// writing the output to wr.
// If an error occurs executing the template or writing its output,
// execution stops, but partial results may already have been written to
// the output writer.
// A template may be executed safely in parallel.
func (t *Template) Execute(wr io.Writer, data interface{}) error {
    if err := t.escape(); err != nil {
        return err 
    }   
    return t.text.Execute(wr, data)
}

GoDoc demonstrates its usage, calling Execute with its interface{} paramter like this;

data := struct {
    Title string
    Items []string
}{
    Title: "My another page",
    Items: []string{},
}


err = t.Execute(os.Stdout, data)
  • 写回答

1条回答 默认 最新

  • douyan5481 2016-01-09 04:19
    关注

    If you look at the return of Execute it calls t.text.Execute which is calling this function from the text/template package:

       132  func (t *Template) Execute(wr io.Writer, data interface{}) (err error) {
       133      defer errRecover(&err)
       134      value := reflect.ValueOf(data)
       135      state := &state{
       136          tmpl: t,
       137          wr:   wr,
       138          vars: []variable{{"$", value}},
       139      }
       140      if t.Tree == nil || t.Root == nil {
       141          state.errorf("%q is an incomplete or empty template%s", t.Name(), t.DefinedTemplates())
       142      }
       143      state.walk(value, t.Root)
       144      return
       145  }
    

    So it will use the reflect package to parse the variables into the template.

    Here's a little example of whats happening: http://play.golang.org/p/ih1Ei33NsO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?