Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of HTML template for Multi-valued substitution in Go language

Shulou Source: shulou.com Published: 2022-06-03 11:55:24 09月30日 Update

This article will explain in detail the HTML template example analysis of Go language multi-valued substitution, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Multivariable value substitution based on HTML templates is provided in two ways. An example of array iteration is attached.

Input map to realize multi-value substitution

The code is as follows:

Package mainimport ("html/template"os") func main () {t, _: = template.New ("demo") .Parse (`{{define "T"}} Hello, {{.username}}! MainPage: [{{.MainPage}}] {{end}} `) args1: = map [string] string {"Username": "Hypermind", "MainPage": "http://hypermind.com.cn/go"} _ = t.ExecuteTemplate (os.Stdout," T ", args1)}

Pass in custom structure to realize multi-valued substitution

The code is 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}}! MainPage: [{{.MainPage}}] {{end}} `)

Args2: = Info {Username: "Hypermind", MainPage: "http://hypermind.com.cn/go"}

_ = t.ExecuteTemplate (os.Stdout, "T", args2)

}

Iterative display of two-dimensional array

The copy code is as follows:

Package mainimport ("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)} sample analysis of HTML templates on multi-valued substitution in Go language is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Code templates examples languages analysis content arrays articles more knowledge articles iterations good way structure quality values two-dimensional reference help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MySQL vpn OPPO Reno Docker