编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Golang Web编程,array数组、Slice切片、Map集合、Struct结构体

wxchong 2024-11-11 14:35:41 开源技术 69 ℃ 0 评论

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>

执行结果

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表