The HTML template instance analysis for Go language multivalued replacement _golang

Source: Internet
Author: User

This article analyzes the use of HTML templates for Go language multiple-value substitution. Share to everyone for your reference. Specifically as follows:

There are two ways to provide multiple variable value substitutions based on HTML templates. An additional example of an array iteration.

Multi-value substitution for incoming map implementation

Copy Code code as follows:
Package Main
Import (
"Html/template"
"OS"
)
Func Main () {
T, _: = template. New ("Demo"). Parse (' {{define ' T '}}hello, {{. username}}! Main Page: [{{. Mainpage}}]{{end}} ')
ARGS1: = map[string]string {"Username": "Hypermind", "MainPage": "Http://hypermind.com.cn/go"}
_ = t.executetemplate (OS. Stdout, "T", ARGS1)
}

Implementing multiple value substitution in incoming custom structure

Copy Code code as follows:
Package Main
Import (
"Html/template"
"OS"
)
Type Info struct{
Username string
MainPage string
}
Func Main () {
T, _: = template. New ("Demo"). Parse (' {{define ' T '}}hello, {{. username}}! Main Page: [{{. Mainpage}}]{{end}} ')
ARGS2: = info{username: "Hypermind", MainPage: "Http://hypermind.com.cn/go"}
_ = t.executetemplate (OS. Stdout, "T", ARGS2)
}

Iterative display of two-dimensional arrays

Copy Code code as follows:
Package Main
Import (
"Html/template"
"OS"
)
Type Matrix struct {
Array [9][9]int
}
Func Main () {
Tmpl, _: = template. New ("Example"). Parse ('
{{{$a: =. Array}}
{{range $a}}} {{{$elem: =.}}| {{Range $elem}}} {{printf '%d '.}} {{end}}|
{{end}} ')
Tmpl. Execute (OS. Stdout, Matrix)
}

I hope this article will help you with your go language program.

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.