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")
}
}
html模板if
<!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>
模板for循环
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"}} //在当前模板中定义了另外一个模板,这里是模板命名

随机推荐

  1. 39 多线程(十一)——ThreadLocal

    目前阶段,我只能知其然,不能做到知其所以然,这里引用一篇其所以然的文章,为以后理解ThreadLocal做准备: https://www.cnblogs.com/ldq2016/p/9041856.h ...

  2. 17 IO流(十四)——Print流

    PrintStream流 PrintStream作为一个包装流,它可以包装字节流,甚至可以使用指定的文件创建一个打印流.它的构造函数很丰富,建议打开API看一下. 它常用的方法是print方法与pri ...

  3. docker自动化脚本

    使用脚本从git上拉取项目并运行, 有些不足的地方 编写脚本 run.sh 如果用到redis和myslq,要先启动redis和mysql #!/bin/bash # author:qiao # 更新 ...

  4. warning: LF will be replaced by CRLF in application.yml. The file will have its origina解决方法

    环境: windows提交时报错如图所示: 原因是存在符号转义问题 windows中的换行符为 CRLF, 而在linux下的换行符为LF,所以在执行add . 时出现提示,解决办法: git con ...

  5. 用JS实现一个斗地主发牌器

    //调用随机数,在我上一篇博文讲过这一个函数. function roundNum(min = 0, max = 0) { if (!isNaN(min) && !isNaN(max) ...

  6. 关于vue-svg-icon的使用方式

    前言 工作中用到svg格式的图标,既然是svg,当然不想用古老的img方式引用,希望能凭借定义svg的fill属性,随意定义图标的颜色:同时不想将整段svg代码写入组建内,于是找到了使用vue-svg ...

  7. AE二次开发,解决子窗体使用父窗体的AxControl控件

    在子窗体写构造函数,然后再在父窗体按钮点击事件下写 public frmIDW(AxMapControl axMapControl1) { InitializeComponent(); this.ax ...

  8. MySQL Backup--Xtrabackup备份参数

    Xtrabackup备份参数 参数选项: innobackupex [--compress] [--compress-threads=NUMBER-OF-THREADS] [--compress-ch ...

  9. 宁波市第二届CTF之cripto1

    密码学第一题 给的是一个shadow文件,16进制编辑器打开 是一串Linux root用户密码的密文,猜测密码可能就是flag,于是将这一串字符放到文本. linux下爆破用户密码的工具没接触过(还 ...

  10. C++(三十六) — 等号操作符重载、&&,||不可重载

    1.等号操作符重载,实现深拷贝 //等号运算符重载 // obj3=obj1;//原始的是浅拷贝,现在要重载为深拷贝 Name& operator=(Name &obj1) { //1 ...