go html
2024-08-22 14:21:29
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"}} //在当前模板中定义了另外一个模板,这里是模板命名
最新文章
- Spring注解学习
- 4. javacript高级程序设计-变量、作用域和内存问题
- 【DPM】Deformable Part Models matlab代码在windows下的调试过程
- 【python】datetime获取日期,前一天日期
- Open vSwitch安装及配置
- 分享一个MVC的多层架构,欢迎大家拍砖斧正
- onclick跳转
- jquery 父页面 子页面 同级页面 调用
- 自己动手写谷歌API翻译接口
- 【Android Developers Training】 41. 向另一台设备发送文件
- Java的字符串分割的不同实现
- python之sqlalchemy的使用
- Epemme
- Ubuntu 18.04 Server上安装LAMP
- table tr 加入背景色之后 去掉td之间的空隙
- 如何将指定文件或文件夹直接提交到svn指定目录
- neo4j----创建索引
- eth0 eth0:1 eth0.1 的区别
- Scalable IO in Java【java高效IO】
- 微信小程序退款 处理类
热门文章
- bzoj 4500 矩阵 题解
- java笔记——java加载与执行
- fastjson转换对象时出错,";$ref";: ";$.data.list[0].xxxx";
- 【C#】上机实验七
- Oracle中的dual表简介
- 解决springboot 新版本 2.1.6 spring-boot-starter-actuator 访问报404
- Harbor 企业级私有仓库 Ubuntu16.04 搭建及使用
- pytorch 0.4.0迁移指南
- AD域控服务器更改IP地址步骤
- PLSQL登录失败出现空白错误提示框的问题