This is a creation in Article, where the information may have evolved or changed.
Go language template, Text/template package
Defined
A template is a set of text embedded in another set of text
The simplest replacement for incoming string–
Package Mainimport ( "OS" "Text/template") func main () { Name: = "Waynehu" tmpl, err: = template. New ("Test"). Parse ("Hello, {{.}}")//Create a template with the content "Hello, {{.}}" If err! = Nil { panic (err) } err = Tmpl. Execute (OS. Stdout, name) //The string is composited with the template, and the contents of the variable name are replaced by {{.} } The compositing results are placed on the OS. StdOut if err! = Nil { panic (err) } }//output: Hello, Waynehu
Because it "hello, {{.}}"
is also a string, it can be carried out separately, as follows:
This sentence tmpl, err: = template. New ("Test"). Parse ("Hello, {{.}}") equals the following two sentences muban: = "Hello, {{.}}" Tmpl, err: = template. New ("Test"). Parse (muban)//The following example is expressed in two sentences
Incoming struct
Template composition that sentence, the 2nd parameter is interface{}, so you can pass in any type, now pass in the struct to see
To get the value of a struct, just use the member name to see the code:
Package Mainimport ( "OS" "Text/template") type Inventory struct { Material string Count UINT} Func Main () { Sweaters: = inventory{"wool", +} muban: = "{{. Count}} items are made of {{. Material}} " Tmpl, err: = template. New ("Test"). Parse (muban) //Create a template if err! = Nil { panic (err) } err = Tmpl. Execute (OS. Stdout, sweaters)//The struct is synthesized with the template and the result is placed on the OS. StdOut if err! = Nil { panic (err) } }//output: + items are made of wool
Multi-template, Introduction new,name,lookup
A template can have multiple, with name to differentiate Muban_eng: = "{. Count}} items are made of {{. Material}} "Muban_chn: =" {{. Material}} has done {{. Count}} Items "//create a template with the name of China, the content of the template is Muban_chn string Tmpl, err: = template. New ("China") tmpl, err = Tmpl. Parse (MUBAN_CHN)//Create a template with the name of 中文版, the content of the template is Muban_eng string tmpl, err = Tmpl. New ("中文版") tmpl, err = Tmpl. Parse (Muban_eng)//combine the struct with the template, synthesize it with a template named China, and put the result into the OS. STDOUT, content for "wool made 17 items" Err = Tmpl. Executetemplate (OS. Stdout, "China", sweaters)//combine the struct with the template, synthesize it with the template named China, and put the result into the OS. In stdout, the content is "made of wool" err = Tmpl. Executetemplate (OS. Stdout, "中文版", sweaters) tmpl, err = template. New ("中文版") fmt. Println (Tmpl. Name ()) //Print out Englishtmpl, err = Tmpl. New ("China") fmt. Println (Tmpl. Name ()) //print out Chinatmpl=tmpl. Lookup ("中文版")//must have a return, otherwise it will not take effect to FMT. Println (Tmpl. Name ()) //Print out 中文版
File templates, introducing Parsefiles
The template can be one line muban: = "{{. Count}} items are made of {{. Material}} "//can also be multiline muban: = ' Items number is {{. Count}}there made of {{. Material}} '
The content of the template is sent to a text file, when used to assign all the contents of the text file to muban this variable can be
The above ideas can be implemented on their own, but in fact the Tamplate package has helped us encapsulate, that is the template. Parsefiles method
Suppose that there is a file Mb.txt content that is the content of the muban variable $cat mb.txt{{. Count}} items are made of {{. Material}} then the following 2 lines muban: = "{{. Count}} items are made of {{. Material}} "Tmpl, err: = template. New ("Test"). Parse (muban) //Create a template equivalent to Tmpl, err: = template. Parsefiles ("Mb.txt") //Create a template where new ("name") is not required, because name is automatically file name
File templates, introducing Parseglob
Parsefiles accepts a string, the content of the string is the path to a template file (absolute path or relative path)
Parseglob is also similar, is to use regular way to match multiple files
Suppose a directory has a.txt b.txt c.txt words with parsefiles need to write 3 lines corresponding 3 files, if there are 10,000 files? and use Parseglob as long as write Template.parseglob ("*.txt") can
Template output, introducing Executetemplate and Execute
There are multiple templates under the template, with a set of templates that are the current template
You can view the current template by using name
Err = Tmpl. Executetemplate (OS. Stdout, "中文版", sweaters) //Specify the template name, this time for englisherr = Tmpl. Execute (OS. Stdout, sweaters) //template name omitted, the current template is printed
Reuse of templates
Templates can be used to set up templates for reuse purposes, using the template keyword
Muban1: = ' Hi, {{template ' M2 '}},hi, {{template ' M3 '} ' muban2: = "I am a template 2,{{template" M3 "}}" Muban3: = "ha I am template 3ha!" Tmpl, err: = template. New ("M1"). Parse (Muban1) Tmpl. New ("M2"). Parse (muban2) Tmpl. New ("M3"). Parse (muban3) Err = Tmpl. Execute (OS. Stdout, Nil)
Full code:
Package Mainimport ( "OS" "Text/template") func main () { muban1: = ' Hi, {{template ' M2 '}},hi, {{Template ' M3 ' }} ' muban2: = ' I am a template 2,{{template ' M3 '}} ' muban3: = ' ha I am a template 3ha! ' Tmpl, err: = template. New ("M1"). Parse (muban1) if err! = Nil { panic (err) } Tmpl. New ("M2"). Parse (muban2) if err! = Nil { panic (err) } Tmpl. New ("M3"). Parse (MUBAN3) if err! = Nil { panic (err) } err = Tmpl. Execute (OS. Stdout, nil) if err! = Nil { panic (err) } }
Content of the output
Hi, I am template 2,ha I am template 3ha!,hi, ha I am template 3ha!
Carriage return of the template
The return of the template file is also part of the template, if the return position control is not good, the composition of the article will be distorted
The example (Template) in the standard library is still a bit messy, and I'll tidy it up as follows:
Const LETTER = ' Dear {{. Name}},{{if. Attended}}it was a pleasure to see you at the wedding. If attended is true, this is the second line {{Else}}it is a shame you couldn ' t make It t o The wedding. If attended is false, this sentence is the second line {{End}}{{with. Gift}}thank for the lovely {{.}}. {{End}} Best Wishes,josie '
Explain:
Dear某某某
The dear should be on the first line, so D
you can't have a carriage return in front, or you Dear
'll run to line 2nd.
- So keep it close.
Dear
\`
- The name of the letter obeyed and the body has a line of blank line, it is best to explicitly hit a line, and the standard library of carriage return is wrapped in the if, as part of the text, so the layout error-prone
- The correct text layout is as follows
- If the body is a line, write all the contents of true and false in one line
- For example, {{if. Attended}}true Line,hello true{{else}}false Line,hi False{{end}}
- If the body has more than one line, it is equivalent to splitting a row into multiple lines
- You will find that the first line of true last line and false is on the same line
- {{If.} attended}} and the first line of ture on the same line
- The last line of {{end}} and False is on the same line
As follows
{{If.} Attended}}true Linehello true{{else}}false Linehi False{{end}}
- About
{{with .Gift}}
, meaning that if gift is not empty, the entire line is printed, and if it is empty, it is not printed
- Only in this way, with the corresponding end to write in the 2nd line, will be "Thank you" after the sentence with a carriage enter, so the wording, like "Thank You" This sentence is inserted in the text below the
- The only way to write, whether there is no "Thank you", the text and best wishes, is always only 1 lines blank