Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report