go template 源码的一个疑问

15次阅读

共计 975 个字符,预计需要花费 3 分钟才能阅读完成。

// Parse parses text as a template body for t.
// Named template definitions ({{define ...}} or {{block ...}} statements) in text
// define additional templates associated with t and are removed from the
// definition of t itself.
//
// Templates can be redefined in successive calls to Parse.
// A template definition with a body containing only white space and comments
// is considered empty and will not replace an existing template's body.
// This allows using Parse to add new named template definitions without
// overwriting the main template body.
func (t *Template) Parse(text string) (*Template, error) {t.init()
	t.muFuncs.RLock()
	trees, err := parse.Parse(t.name, text, t.leftDelim, t.rightDelim, t.parseFuncs, builtins())
	t.muFuncs.RUnlock()
	if err != nil {return nil, err}
	// Add the newly parsed trees, including the one for t, into our common structure.
	for name, tree := range trees {if _, err := t.AddParseTree(name, tree); err != nil {return nil, err}
	}
	return t, nil
}

我认为应该是下面这样,返回值没必要加原来的 *Template

func (t *Template) Parse(text string) error 

注释里面写了 Templates can be redefined in successive calls to Parse.

但是没明白什么场景会 redefined。

正文完
 0