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. XGBoost对波士顿房价进行预测

    import numpy as np import matplotlib as mpl mpl.rcParams["font.sans-serif"] = ["SimHe ...

  2. Go语言( 流程控制)

    流程控制是每种编程语言控制逻辑走向和执行次序的重要部分,流程控制可以说是一门语言的“经脉”. Go语言中最常用的流程控制有if和for,而switch和goto主要是为了简化代码.降低重复代码而生的结 ...

  3. ColorTransform调整显示对象的颜色值

    ColorTransform调整显示对象的颜色值: /** * * *------------------------------* * | *** 调整显示对象的颜色值 *** | * *----- ...

  4. iview 表格随着更改刷新

    使用location.reload() 或者是 路由的 this.$router.go(0) 进行刷新的时候,是会出现一阵的空白区域的,因为是整个页面的刷新 ,所以比较缓慢,因此使用了provide/ ...

  5. Springboot 整合ApachShiro完成登录验证和权限管理

    1.前言 做一个系统最大的问题就是安全问题以及权限的问题,如何正确的选择一个安全框架对自己的系统进行保护,这方面常用的框架有SpringSecurity,但考虑到它的庞大和复杂,大多数公司还是会选择 ...

  6. JDBC使用8.0驱动包连接mysql设置时区serverTimezone

    驱动包用的是新版 mysql-connector-java-8.0.16.jar新版的驱动类改成了com.mysql.cj.jdbc.Driver新版驱动连接url也有所改动I.指定时区 如果不设置时 ...

  7. ADO.NET 八(一个例子)

    可视化方式绑定 DataGridView 控件(写的不详细,结合上一篇) 使用可视化数据绑定方式可以快速完成将数据表中的数据显示在 DataGridView 控件中的操作,并可以很容易地对绑定列的属性 ...

  8. Flutter Image(图片)

    Image是一个用于展示图片的组件.支持 JPEG.PNG.GIF.Animated GIF.WebP.Animated WebP.BMP 和 WBMP 等格式. Image 有许多的静态函数: ne ...

  9. 在notepad++中编辑时光标消失不见

    在notepad++进行编辑时,会不知道的情况下,鼠标光标由竖线变成了下划线,如图 解决方法很简单,是点击”insert“键或者”ins“键,即可改变光标形状.

  10. 笔谈runloop(一)

    关于runloop熟悉而又陌生,熟悉的是在iOS开发的过程中线程的执行就跟runloop扯上关系了,陌生的是runloop到底是个什么东西.去公司面试的时候,很多公司的面试人员会问这个问题.runlo ...