Go Language Html/template parsing problem

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Recently using the Go Language Html/template package to do some static work, found that the template for less than (left angle bracket <) is not properly handled, repeated research, did not understand how the matter, and later modified, the template is escapestring encoded, After formatting, the problem is solved by unescapestring coding;
But I still can't figure out why the template is not properly formatted for a single less than sign (left angle bracket <), but found that if there is one or more right angle brackets (>) on the right side of the left angle bracket, the format can be parsed correctly, and the template source package reads very confusing and ultimately does not understand, Students who want to know can tell the reason;
Stick to my solution for reference

Package Mytemplateimport ("bytes" "HTML" "Html/template" "OS" "StrConv" "Time") type MyTemplate struct {Leftdelim Stringrightdelim stringescape Bool}func newmwtemplate (left, right string, escape bool) *mwtemplate {return &mwtemp Late{left, right, escape,}}//formats the template string//@param tplstr template string//@param tplobj Template Data func (this *mytemplate) tpltostring ( Tplstr string, Tplobj interface{}) (Result string, err Error) {//If you need to encode if this. Escape {tplstr = html. Escapestring (tplstr)}t: = template. New (StrConv. Formatint (time. Now (). Unix ()) if err! = Nil {return}//Overlay template left and right separators if this. Leftdelim! = "" && this. Rightdelim! = "" {t.delims (this. Leftdelim, this. Rightdelim)}t, err = T.parse (TPLSTR) if err! = Nil {return}b: = new (bytes. Buffer) Err = T.execute (b, tplobj) if err! = Nil {Return}result = string (b.string ()) if this. Escape {result = HTML. Unescapestring (Result)}return}//tpltofile after formatting the template string, write the file//@param tplstr template string//@param tplpath saved File//@param tplobj Template data func (this *mytemplate) tpltofile (Tplstr, TPLPAth string, Tplobj interface{}) (err error) {result, err: = this. Tpltostring (TPLSTR, tplobj) if err! = Nil {return}fout, err: = OS. Create (Tplpath) if err! = Nil {Return}defer fout. Close () _, err = Fout. WriteString (Result) return}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.