前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang语言社区--模板的使用

Golang语言社区--模板的使用

作者头像
李海彬
发布2018-03-21 10:39:25
6580
发布2018-03-21 10:39:25
举报
使用Parse
package main
import (
    "html/template"
    "net/http"
)
func SayHello(w http.ResponseWriter, req *http.Request) {
    name := "dabin"
    tmpl, _ := template.New("TXT").Parse("大家好,我是{{.}}")
    tmpl.Execute(w, name)
}
func main() {
    http.HandleFunc("/", SayHello)
    http.ListenAndServe(":80", nil)
}
使用ParseFiles
go代码
package main
import (
    "html/template"
    "net/http"
)
type Info struct {
    Title string
    Name  string
    Site  string
}
func SayHello(w http.ResponseWriter, req *http.Request) {
    info := Info{"个人网站", "dabin", "www.golangweb.com"}
    tmpl, _ := template.ParseFiles("home.html")
    tmpl.Execute(w, info)
}
func main() {
    http.HandleFunc("/", SayHello)
    http.ListenAndServe(":80", nil)
}
home.html代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>{{.Title}}</title>
</head>
<body>
大家好,我是{{.Name}}。这是我的网站{{.Site}}
</body>
</html>
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-04-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档