如何解决Golang从文件中嵌入HTML?
template.ParseFiles()template.ParseGlob()
index.htmlheader.html
<html>
<head lang="en">
</head>
<body>
<header>{{template "header.html"}}</header>
<div class="panel panel-default">
</div>
</body>
</html>
"index.html"
t, err := template.ParseFiles("index.html", "header.html")
if err != nil {
panic(err)
}
err = t.ExecuteTemplate(os.Stdout, "index.html", nil)
if err != nil {
panic(err)
}
template.ParseGlob()
t, err := template.ParseGlob("*.html")
// ...and the rest is the same...
输出(打印在控制台上):
<html>
<head lang="en">
</head>
<body>
<header><div id="logo"></div><div id="motto"></div></header>
<div class="panel panel-default">
</div>
</body>
</html>