Go語(yǔ)言多值替換的HTML模板實(shí)例分析
本文實(shí)例分析了Go語(yǔ)言多值替換的HTML模板用法。分享給大家供大家參考。具體如下:
這里通過(guò)兩種方式提供基于HTML模板的多變量值替換。另外附加一個(gè)數(shù)組迭代的示例。
傳入map實(shí)現(xiàn)多值替換
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)
}
傳入自定義結(jié)構(gòu)實(shí)現(xiàn)多值替換
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)
}
二維數(shù)組的迭代顯示
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)
}
希望本文所述對(duì)大家的Go語(yǔ)言程序設(shè)計(jì)有所幫助。
相關(guān)文章
golang 實(shí)現(xiàn)interface{}轉(zhuǎn)其他類型操作
這篇文章主要介紹了golang 實(shí)現(xiàn)interface{}轉(zhuǎn)其他類型操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12Go語(yǔ)言reflect.TypeOf()和reflect.Type通過(guò)反射獲取類型信息
這篇文章主要介紹了Go語(yǔ)言reflect.TypeOf()和reflect.Type通過(guò)反射獲取類型信息,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04創(chuàng)建第一個(gè)Go語(yǔ)言程序Hello,Go!
這篇文章主要介紹了創(chuàng)建第一個(gè)Go語(yǔ)言程序Hello,Go!本文詳細(xì)的給出項(xiàng)目創(chuàng)建、代碼編寫的過(guò)程,同時(shí)講解了GOPATH、Go install等內(nèi)容,需要的朋友可以參考下2014-10-10Go中函數(shù)的使用細(xì)節(jié)與注意事項(xiàng)詳解
在Go語(yǔ)言中函數(shù)可是一等的(first-class)公民,函數(shù)類型也是一等的數(shù)據(jù)類型,下面這篇文章主要給大家介紹了關(guān)于Go中函數(shù)的使用細(xì)節(jié)與注意事項(xiàng)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11golang 后臺(tái)進(jìn)程的啟動(dòng)和停止操作
這篇文章主要介紹了golang 后臺(tái)進(jìn)程的啟動(dòng)和停止操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04十個(gè)Golang開發(fā)中應(yīng)該避免的錯(cuò)誤總結(jié)
Go是一種靜態(tài)類型的、并發(fā)的、垃圾收集的編程語(yǔ)言,由谷歌開發(fā)。開發(fā)人員在編寫Go代碼時(shí)總會(huì)有一些常見的錯(cuò)誤,下面是Go語(yǔ)言中需要避免的十大壞錯(cuò)誤,希望對(duì)大家有所幫助2023-03-03