标签:serve 一个 The edit 命名 splay iter code top
package main
import (
"fmt"
"html/template"
"net/http"
)
type User struct {
UserName string
Age int
}
func info(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("F:\\GoDevelopment\\src\\gocode\\project01\\templates\\info.html")
if err != nil {
fmt.Println("open html file failed", err)
return
}
//data := "80天环游世界"
user := User{
"豪杰",
18,
}
t.Execute(w, user)
}
func main() {
http.HandleFunc("/", info)
err := http.ListenAndServe("0.0.0.0:5000", nil)
if err != nil {
panic("start http server failed,err")
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的个人中心</title>
</head>
<body>
<div>
<ul>
<li>《linux内核》</li>
<li>姓名{{.UserName}}</li>
<li>年龄{{.Age}}</li>
</ul>
{{/* go模板中的注释*/}}
{{ $age :=.Age}}
{{ $age }}
{{if gt .Age 18}}
<div>
"澳门首家赌场开业啦"
</div>
{{ else }}
<div>
<div>
快乐成长
</div>
</div>
{{endif}}
</div>
</body>
</html>
package main
import (
"fmt"
"html/template"
"net/http"
)
type User struct {
UserName string
Age int
}
func info(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("F:\\GoDevelopment\\src\\gocode\\project01\\templates\\info.html")
if err != nil {
fmt.Println("open html file failed", err)
return
}
//data := "80天环游世界"
userMap := map[int]User{
1:{"托尼",11},
2:{"天哥",12},
3:{"杰少",19},
}
t.Execute(w, userMap)
}
func main() {
http.HandleFunc("/", info)
err := http.ListenAndServe("0.0.0.0:5000", nil)
if err != nil {
panic("start http server failed,err")
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的个人中心</title>
</head>
<body>
<div>
<table border="1">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
{{ range $index,$user:= .}}
<tr>
<td>{{$index}}</td>
<td>{{$user.UserName}}</td>
<td>{{$user.Age}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</body>
</html>
<p>map 长度: {{len .}}</p>
<p>{{index . 1}}</p>
{{with index . 1}}<p>
{{ printf "姓名:%s 年龄:%d" .UserName .Age}}</p>
{{end}}
package main
import (
"html/template"
"io/ioutil"
"net/http"
)
type User struct {
UserName string
Age int
}
func info(w http.ResponseWriter, r *http.Request) {
//添加自定义的方法要在parse模板文件之前添加
kuaFunc := func(arg string) (string, error) {
return arg + "真帅", nil
}
htmlByte, err := ioutil.ReadFile("F:\\GoDevelopment\\src\\gocode\\project01\\templates\\info.html")
if err != nil {
return
}
//template.New("info") 创建一个Templates对象
templ, err := template.New("info").Funcs(template.FuncMap{"kua": kuaFunc}).Parse(string(htmlByte))+
if err != nil {
return
}
//data := "80天环游世界"
userMap := map[int]User{
1: {"托尼", 11},
2: {"天哥", 12},
3: {"杰少", 19},
}
templ.Execute(w, userMap)
}
func main() {
http.HandleFunc("/", info)
err := http.ListenAndServe("0.0.0.0:5000", nil)
if err != nil {
panic("start http server failed,err")
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的个人中心</title>
</head>
<body>
<div>
<table border="1">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
{{ range $index,$user:= .}}
<tr>
<td>{{$index}}</td>
<td>{{$user.UserName}}</td>
<td>{{$user.Age}}</td>
</tr>
{{end}}
</tbody>
</table>
<p>map 长度: {{len .}}</p>
<p>{{index . 1}}</p>
{{with index . 1}}
<p>{{ printf "姓名:%s 年龄:%d" .UserName .Age }}</p>
<p>{{ kua .UserName}}</p>
{{end}}
</div>
</body>
</html>
template方法
{{ template "ol.html"}} //在当前模板调用另外一个模板
{{ define "ol.html"}} //在当前模板中定义了另外一个模板,这里是模板命名
标签:serve 一个 The edit 命名 splay iter code top
原文地址:https://www.cnblogs.com/hualou/p/12069878.html