网站首页 > 开源技术 正文
for rang语句 :实现遍历数据功能,可以实现对数组、Slice、Map、或Channel等数据结构
. 表示每次迭代循环中的元素
- 切片的循环
main.go源码及解析
package main
import (
"net/http"
"text/template"
)
func Index(w http.ResponseWriter, r *http.Request) {
//ParseFiles从"index.html"中解析模板。
//如果发生错误,解析停止,返回的*Template为nil。
//当解析多个文件时,如果文件分布在不同目录中,且具有相同名字的,将以最后一个文件为主。
files, _ := template.ParseFiles("index.html")
//声明变量b,类型为字符串切片,有三个内容
b := []string{"张无忌", "张三丰", "张翠山"}
//声明变量b,类型为字符串切片,没有内容,是为了else示例
//b := []string{}
//Execute负责渲染模板,并将b写入模板。
_ = files.Execute(w, b)
//w.Write([]byte("/index"))
}
func main() {
http.HandleFunc("/index", Index)
_ = http.ListenAndServe("", nil)
}
模板index.html的源码及解析
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<pre>
{{range .}}
{{.}}
{{else}}
None
{{end}}
</pre>
</body>
</html>
测试http服务,推荐使用httptest进行单元测试
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestIndex(t *testing.T) {
//初始化测试服务器
handler := http.HandlerFunc(Index)
app := httptest.NewServer(handler)
defer app.Close()
//测试代码
//发送http.Get请求,获取请求结果response
response, _ := http.Get(app.URL + "/index")
//关闭response.Body
defer response.Body.Close()
//读取response.Body内容,返回字节集内容
bytes, _ := ioutil.ReadAll(response.Body)
//将返回的字节集内容通过string()转换成字符串,并显示到日志当中
t.Log(string(bytes))
}
执行结果
- map的循环
package main
import (
"net/http"
"text/template"
)
func Index(w http.ResponseWriter, r *http.Request) {
//ParseFiles从"index.html"中解析模板。
//如果发生错误,解析停止,返回的*Template为nil。
//当解析多个文件时,如果文件分布在不同目录中,且具有相同名字的,将以最后一个文件为主。
files, _ := template.ParseFiles("index.html")
//声明变量b,类型为map切片,有三个内容
b := []map[string]interface{}{
{"id": 1, "name": "张无忌", "age": 18},
{"id": 2, "name": "周芷若", "age": 16},
{"id": 3, "name": "谢逊", "age": 39},
}
//Execute负责渲染模板,并将b写入模板。
_ = files.Execute(w, b)
//w.Write([]byte("/index"))
}
func main() {
http.HandleFunc("/index", Index)
_ = http.ListenAndServe("", nil)
}
模板index.html的源码及解析
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<pre>
{{range .}}
{{.id}} {{.name}} {{.age}}
{{else}}
None
{{end}}
</pre>
</body>
</html>
执行结果
- 结构体的循环
package main
import (
"net/http"
"text/template"
)
// username 需要注意的是,如果采用结构体类型,那么就要考虑字段的首字母必须大写
type username struct {
Id int
Name string
Age int
}
func Index(w http.ResponseWriter, r *http.Request) {
//ParseFiles从"index.html"中解析模板。
//如果发生错误,解析停止,返回的*Template为nil。
//当解析多个文件时,如果文件分布在不同目录中,且具有相同名字的,将以最后一个文件为主。
files, _ := template.ParseFiles("index.html")
//声明变量b,类型为结构体切片,有三个内容
u := []username{
{Id: 1, Name: "张无忌", Age: 18},
{Id: 2, Name: "周芷若", Age: 16},
{Id: 3, Name: "谢逊", Age: 39},
}
//Execute负责渲染模板,并将b写入模板。
_ = files.Execute(w, u)
//w.Write([]byte("/index"))
}
func main() {
http.HandleFunc("/", Index)
_ = http.ListenAndServe("", nil)
}
模板index.html的源码及解析
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<pre>
首字母大写
{{range .}}
{{.Id}} {{.Name}} {{.Age}}
{{else}}
None
{{end}}
</pre>
</body>
</html>
执行结果
- 数组的循环
package main
import (
"log"
"net/http"
"text/template"
)
// username 需要注意的是,如果采用结构体类型,那么就要考虑字段的首字母必须大写
type username struct {
Id int
Name string
Age int
}
func Index(w http.ResponseWriter, r *http.Request) {
//ParseFiles从"index.html"中解析模板。
//如果发生错误,解析停止,返回的*Template为nil。
//当解析多个文件时,如果文件分布在不同目录中,且具有相同名字的,将以最后一个文件为主。
files, _ := template.ParseFiles("index.html")
//声明变量b,类型为数组切片,有三个内容
var arr = [][]interface{}{
{1, "张无忌", 18},
{2, "赵敏", 16},
{3, "张翠山", 38},
}
log.Println(arr)
//Execute负责渲染模板,并将b写入模板。
_ = files.Execute(w, arr)
//w.Write([]byte("/index"))
}
func main() {
http.HandleFunc("/", Index)
_ = http.ListenAndServe("", nil)
}
模板index.html的源码及解析
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<pre>
{{.}}
{{range .}}
{{index . 0}} {{index . 1}} {{index . 2}}
{{else}}
None
{{end}}
</pre>
</body>
</html>
执行结果
猜你喜欢
- 2024-11-11 Golang Web编程,模板解析 if、else if、else语句
- 2024-11-11 Go语言Web编程,Request查询参数URL Query GET
- 2024-11-11 模玩资讯:千值练《机动机器人WeGo》第四弹 微型 MechatroWego 盒玩
- 2024-11-11 Go语言编程从入门到精通(字符串创建、拼接、转换、替换、查找)
- 2024-11-11 Go语言编程从入门到精通,方法和接口
- 2024-07-25 Go语言编程从入门到精通,数据类型:布尔、数值、浮点、字符串
- 2024-07-25 Go语言编程从入门到精通(通道、缓冲通道、阻塞、控制、select)
- 2024-07-25 Go语言编程从入门到精通,流程控制之switch、for、defer
- 2024-07-25 Golang并发编程,4、无缓冲通道和有缓冲通道的区别
- 2024-07-25 golang web从入门到精通 1篇文章学习Cookie增加、删除、修改、查询
你 发表评论:
欢迎- 03-19基于layui+springcloud的企业级微服务框架
- 03-19开箱即用的前端开发模板,扩展Layui原生UI样式,集成第三方组件
- 03-19SpringMVC +Spring +Mybatis + Layui通用后台管理系统OneManageV2.1
- 03-19SpringBoot+LayUI后台管理系统开发脚手架
- 03-19layui下拉菜单form.render局部刷新方法亲测有效
- 03-19Layui 遇到的坑(记录贴)(layui chm)
- 03-19基于ASP.NET MVC + Layui的通用后台开发框架
- 03-19LayUi自定义模块的定义与使用(layui自定义表格)
- 最近发表
-
- 基于layui+springcloud的企业级微服务框架
- 开箱即用的前端开发模板,扩展Layui原生UI样式,集成第三方组件
- SpringMVC +Spring +Mybatis + Layui通用后台管理系统OneManageV2.1
- SpringBoot+LayUI后台管理系统开发脚手架
- layui下拉菜单form.render局部刷新方法亲测有效
- Layui 遇到的坑(记录贴)(layui chm)
- 基于ASP.NET MVC + Layui的通用后台开发框架
- LayUi自定义模块的定义与使用(layui自定义表格)
- Layui 2.9.11正式发布(layui2.6)
- Layui 2.9.13正式发布(layui2.6)
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)