A concise tutorial on Golang template Syntax (benefits later)

Source: Internet
Author: User
Tags define local
This is a creation in Article, where the information may have evolved or changed. Template is an essential part of the Go language web development and is hereby recorded: "{{" and "}}" are enclosed in the "Templates label" template label   "comments" {{/* a comment * *}} using "{{* *" and "*}}" to include the comment content   " Variable "{{.}} This label outputs the value of the current object, {{. Admpub}} represents the value of the field or method name "Admpub" in the output struct object. When "Admpub" is an anonymous field, you can access its internal fields or methods, such as "Com": {{. Admpub.com}}, if "Com" is a method and returns a struct object, it can also access its fields or methods: {. admpub.com.field1}}{{. Method1 "parameter value 1" "Parameter Value 2"}} Call Method "Method1", pass the following parameter value to this method sequentially, and output its return value. {{$admpub}} This label is used to output a variable named "admpub" defined in the template. When $admpub itself is a struct object, it can access its fields: {{$admpub. Field1}} defines a variable in a template: The variable name is made up of letters and numbers, with a "$" prefix, and is assigned with the symbol ": =". For example: {{$x: = "OK"}} or {{$x: = pipeline}}  "pipe function" usage 1:{{funcname1}} This tag will invoke a template function named "FuncName1" (equivalent to executing "FuncName1 ()", does not pass any parameters) and outputs its return value. Usage 2:{{funcname1 "parameter value 1" "Parameter Value 2"}} This tag will call "FUNCNAME1 (" parameter value 1 "," parameter Value 2 ")" and output its return value usage 3:{{. admpub| FUNCNAME1}} This tag invokes the template function named "FuncName1" (equivalent to the execution "FuncName1 (this). Admpub) ", the vertical bar" | " To the left. " The admpub "variable value is passed as a function parameter and outputs its return value.   "Conditional judgment" usage 1:{{if pipeline}} T1 {{end}} tag structure: {{if ...}} ... {{End}} usage 2:{{if pipeline}} T1 {{else}} T0 {{end}} tag structure: {{if ...}} ... {{Else}} ... {{End}} usage 3:{{if pipeline}} T1 {{else if pipeline}} T0 {{end}} tag structure: {{if ...}} ... {Else If ...}} ... {{end}} after which if can be a conditional expression, including a pipe function expression. Pipeline is a pipeline), or it can be a character channeling variable or a Boolean variable. When the character channeling variable, such as an empty string is judged to be false, otherwise it is judged true.   "Traversal" usage 1:{{range $k, $v: =. Var}} {{$k}} + {{$v}} {{{{{}}} {{{}} {End}}range...end inside the structure to use external variables, for example. VAR2, need to write this: $. Var2 (i.e., "$" is added before the name of the external variable, a separate "$" meaning is equivalent to the global) usage 2:{{range. Var}} {{.}} {{End}} usage 3:{{range pipeline}} T1 {{else}} T0 {{end}} when there are no traversed values, the Else section is executed.   Embed child template usage 1:{{template "name"}} embed a child template with name "name". Before using, make sure that the child template content has been defined with "{{define" name "}} Child template content {{end}}". Use 2:{{template "name" pipeline} to assign the value of the pipe to "." In the child template. (That is, "{{.}}"   "Child template nesting" {{define "T1"}}one{{end}}{{define "T2"}}two{{end}}{{define "T3"}}{{template "T1"}} {{Template "T2"}}{{ End}}{{template "T3"}} output: One two  "define local variables" Usage 1:{{with pipeline}} T1 {{end}} The value of the pipe will be assigned to "." Inside the label. (Note: The word "inside" here refers to being {{with pipeline}} ... {{End}} enclosing part, i.e. T1 location) usage 2:{{with Pipeline}} T1 {{else}} T0 {{end}} if the value of the pipe is empty, "." is unaffected and executes T0, otherwise, assigns the value of the pipe to "." and execute T1.    Description: The {{end}} tag is the end tag for if, with, range.       "Example: Output character channeling" {{"\" output\ "}} outputs a character channeling constant.  {{' "Output" '} ' outputs an original string constant  {{printf "%q" "Output"}} function call. (Equivalent to: printf ("%q", "output"). )  {{"Output" | printf "%q"}} vertical Bar "|" The result on the left is the last parameter of the function. (Equivalent to: printf ("%q", "output").  {{printf "%q" (print "Out" "Put")}} The overall result of the expression in parentheses as an argument to the printf function. (Equivalent to: printf ("%q", Print ("Out", "put").)  {{"put" | printf "%s%s" "Out" | printf "%q"} a more complex call. (Equivalent to: printf ("%q", printf ("%s%s", "Out", "put")).  {{"Output" | printf "%s" | printf "%q"}} is equivalent to: printf ("%q", printf ("%s", "output")).  {{with "Output"}}{{printf "%q".}} {{END}} a use dot "." With operations. (Equivalent to: printf ("%q", "output"). )  {{with $x: = "Output" | printf "%q"}}{{$x}}{{end}}with structure, defines the variable, the value is the result after the execution of the pipe function (equivalent to: $x: = printf ("%q", "output").  {{with $x: = "Output"}}{{printf "%q" $x}}{{end}}with structure, use defined variables  {{with $x in other actions: = "Output"}}{{$x | printf "% Q "}}{{end}}", but the pipeline is used. (Equivalent to: printf ("%q", "output").   =============== "Predefined template global function" ================ "and" {{and x Y}} means: If x then y else x asFruit x is true, returns Y, otherwise returns x. Equivalent to Golang in: x && y  "call" {{call}. X.Y 1 2}} indicates: Dot. X.Y (1, 2) The result of the first argument after call must be a function (that is, it is a value of a function type), and the remaining parameters are the parameters of the function. The function must return one or two result values, where the second result value is the error type. If the passed parameter does not match the function definition or the error value returned is not nil, execution is stopped.   HTML tags in "HTML" escaped text, such as "<" escaped to "&lt;", ">" Escaped to "&gt;"   "Index" {{index x 1 2 3}} returns the element value corresponding to an index of the first parameter after index, and the remaining parameters are indicated by the index value: x[1][2][3]x must be a map, slice, or array   "JS" Returns a Boolean negative value for a single parameter with the text   "Len" returned by the escape process with JavaScript, the length value of the parameter (int type)   "not".   "or" {{or x Y}} means: If x then x else y. Equivalent to Golang in: x | | Y if X is true returns x, otherwise y is returned.   "Print" FMT. The name of the sprint   "printf" FMT. sprintf aliases   "println" FMT. Sprintln aliases   "Urlquery" returns the text escape values that are appropriate for embedding into formal parameters in a URL query. (Similar to PHP urlencode)   ================= "Boolean function" =============== Boolean function returns false for any 0 value, and a non-0 value returns TRUE. This defines a set of binary comparison operator functions:  "EQ" return expression "arg1 = = Arg2" boolean value   "NE" return expression "Arg1! = arg2" boolean   "LT" Return expression "Arg1 < Arg2" Boolean value   "Le" returns the boolean value of the expression "Arg1 <= arg2"   "GT" returns the Boolean value of the expression "Arg1 > arg2"   "GE" returns the Boolean value of the expression "Arg1 >= arg2"   for simple multi-path equality testing, EQ only acceptsTwo parameters are compared, the other parameters will be compared with the first parameter, respectively, {{eq arg1 arg2 arg3 Arg4}} can only be compared as follows: ARG1==ARG2 | | Arg1==arg3 | | Arg1==arg4 ... Source: http://www.admpub.com/blog/post-221.html benefits Come, send you a few cute expression pack, code code, and small partners to fight Map!!! Cheer up ...... , .....
Related Article

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.