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.