From 7ea088b7e312974491d7754b8b5fcfdad00a4144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=AC=E5=AD=A4=E4=BC=B6=E4=BF=9C?= <1184662350@qq.com> Date: Sat, 17 Dec 2022 13:49:45 +0800 Subject: [PATCH] =?UTF-8?q?day9=E6=8E=A5=E5=8F=A3=20=E7=BB=83=E4=B9=A07.9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/study/day9Interface/Practice/7.8.go | 30 +------- src/study/day9Interface/Practice/7.9.go | 73 +++++++++++++++++++ src/study/day9Interface/Practice/comm/comm.go | 30 ++++++++ 3 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 src/study/day9Interface/Practice/7.9.go create mode 100644 src/study/day9Interface/Practice/comm/comm.go diff --git a/src/study/day9Interface/Practice/7.8.go b/src/study/day9Interface/Practice/7.8.go index 2c4335e..e1df4c5 100644 --- a/src/study/day9Interface/Practice/7.8.go +++ b/src/study/day9Interface/Practice/7.8.go @@ -1,6 +1,7 @@ package main import ( + "Study/src/study/day9Interface/Practice/comm" "fmt" "reflect" "sort" @@ -17,37 +18,10 @@ import ( // 4. 实现点击一栏就对一栏排序 // 1. 定义数据类型 -type Person struct { - Name string - Age int - Sex bool - ID int -} -type PersonSlice []Person - -// Less 没有实现sort.Interface的Less方法 -func (p PersonSlice) Less(i, j int, field string) bool { - return PersonByField(p[i], p[j], field) -} - -// PersonByField 按照字段排序 -func PersonByField(i, j Person, field string) bool { - switch field { - case "Name": - return i.Name > j.Name - case "Age": - return i.Age < j.Age - case "Sex": - return i.Sex != j.Sex - case "ID": - return i.ID < j.ID - } - return false -} func main() { // 构建数据 - data := PersonSlice{ + data := comm.PersonSlice{ {"a", 1, true, 1}, {"b", 2, false, 2}, {"c", 3, true, 3}, diff --git a/src/study/day9Interface/Practice/7.9.go b/src/study/day9Interface/Practice/7.9.go new file mode 100644 index 0000000..edd13b7 --- /dev/null +++ b/src/study/day9Interface/Practice/7.9.go @@ -0,0 +1,73 @@ +package main + +import ( + "Study/src/study/day9Interface/Practice/comm" + "html/template" + "log" + "net/http" + "os" + "sort" +) + +// 使用html/template包(§4.6)替代printTracks将tracks展示成一个HTML表格。 +// 将这个解决方案用在前一个练习中,让每次点击一个列的头部产生一个HTTP请求来排序这个表格。 + +const TEMPL = ` + + + + + Document + + + + + + + + + + + {{range .}} + + + + + + + {{end}} +
NameAgeSexID
{{.Name}}{{.Age}}{{.Sex}}{{.ID}}
+ + +` + +func main() { + // 构建数据 + data := comm.PersonSlice{ + {"a", 1, true, 1}, + {"b", 2, false, 2}, + {"c", 3, true, 3}, + {"d", 4, false, 4}, + {"e", 5, true, 5}, + {"f", 6, false, 6}, + {"g", 7, true, 7}, + } + // 构建模版 + templ, err := template.New("test").Parse(TEMPL) + if err != nil { + log.Fatalln(err) + } + templ.Execute(os.Stdout, data) + // 注册web + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // 排序 + sort.Slice(data, func(i, j int) bool { + // 获得sort参数 + return data.Less(i, j, r.FormValue("sort")) + }) + // 输出 + templ.Execute(w, data) + }) + // 监听 + http.ListenAndServe(":8080", nil) +} diff --git a/src/study/day9Interface/Practice/comm/comm.go b/src/study/day9Interface/Practice/comm/comm.go new file mode 100644 index 0000000..ffcb954 --- /dev/null +++ b/src/study/day9Interface/Practice/comm/comm.go @@ -0,0 +1,30 @@ +package comm + +type Person struct { + Name string + Age int + Sex bool + ID int +} + +type PersonSlice []Person + +// Less 没有实现sort.Interface的Less方法 +func (p PersonSlice) Less(i, j int, field string) bool { + return PersonByField(p[i], p[j], field) +} + +// PersonByField 按照字段排序 +func PersonByField(i, j Person, field string) bool { + switch field { + case "Name": + return i.Name > j.Name + case "Age": + return i.Age < j.Age + case "Sex": + return i.Sex != j.Sex + case "ID": + return i.ID < j.ID + } + return false +}