第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

go 'Template.Execute' 如何讀取匿名結(jié)構(gòu)的參數(shù)字段?

go 'Template.Execute' 如何讀取匿名結(jié)構(gòu)的參數(shù)字段?

Go
Qyouu 2021-12-06 19:52:32
在使用 Go 編碼時,使用 html.template 包,調(diào)用 template.Execute。我有一個問題,它如何讀取其參數(shù)的匿名結(jié)構(gòu)字段。我閱讀了源代碼,但沒有意義。我沒有想法。/usr/local/go/src/html/template/tempalte.go L.78type 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 演示了它的用法,像這樣使用其 interface{} 參數(shù)調(diào)用 Execute;data := struct {    Title string    Items []string}{    Title: "My another page",    Items: []string{},}err = t.Execute(os.Stdout, data)
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經(jīng)驗 獲得超10個贊

如果您查看從包中調(diào)用此函數(shù)return的Executeit 調(diào)用:t.text.Executetext/template


   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  }

所以它將使用reflect包將變量解析到模板中。


這是正在發(fā)生的事情的一個小例子:http : //play.golang.org/p/ih1Ei33NsO


查看完整回答
反對 回復(fù) 2021-12-06
  • 1 回答
  • 0 關(guān)注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號