From 414b1b84d9134b12b51324b1446efd4b77958371 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: Thu, 1 Dec 2022 16:54:42 +0800 Subject: [PATCH] =?UTF-8?q?day6=20=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/GithubApi/struct.go | 10 +++---- src/study/day5/surfaceweb/surfaceWEB.go | 2 +- src/study/day6/Template.go | 39 ++++++++++++++++++++++++ src/study/day6/TemplateHTML.go | 40 +++++++++++++++++++++++++ src/study/day6/TemplateWEB.go | 28 +++++++++++++++++ 5 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 src/study/day6/Template.go create mode 100644 src/study/day6/TemplateHTML.go create mode 100644 src/study/day6/TemplateWEB.go diff --git a/pkg/GithubApi/struct.go b/pkg/GithubApi/struct.go index 046db1e..0f001aa 100644 --- a/pkg/GithubApi/struct.go +++ b/pkg/GithubApi/struct.go @@ -12,11 +12,11 @@ type IssuesSearchResult struct { } type Issue struct { - Number int - HTMLURL string `json:"html_url"` - Title string - State string - *User + Number int + HTMLURL string `json:"html_url"` + Title string + State string + User *User CreatedAt time.Time `json:"created_at"` Body string // in Markdown format } diff --git a/src/study/day5/surfaceweb/surfaceWEB.go b/src/study/day5/surfaceweb/surfaceWEB.go index 9b68924..5c2a92f 100644 --- a/src/study/day5/surfaceweb/surfaceWEB.go +++ b/src/study/day5/surfaceweb/surfaceWEB.go @@ -23,7 +23,7 @@ var ConcerError error func main() { http.HandleFunc("/", surface) - log.Fatal(http.ListenAndServe("localhost:8080", nil)) + log.Fatal(http.ListenAndServe("0.0.0.0:8989", nil)) } func surface(w http.ResponseWriter, r *http.Request) { diff --git a/src/study/day6/Template.go b/src/study/day6/Template.go new file mode 100644 index 0000000..771fb46 --- /dev/null +++ b/src/study/day6/Template.go @@ -0,0 +1,39 @@ +package main + +import ( + "Study/pkg/github" + "log" + "os" + "text/template" + "time" +) + +const temple = `{{.TotalCount}} issues: +{{range .Items}}---------------------------------------- +Number: {{.Number}} +User: {{.User.Login}} +Title: {{.Title | printf "%.64s"}} +Age: {{.CreatedAt | daysAgo}} days +{{end}}` + +func dayAgo(t time.Time) float64 { + return time.Since(t).Hours() / 24.0 +} + +func main() { + report, err := template.New("report"). + Funcs(template.FuncMap{"daysAgo": dayAgo}). + Parse(temple) + if err != nil { + log.Fatal(err) + } + + res, err := github.SearchIssues([]string{"go/golang"}) + if err != nil { + log.Fatal(err) + } + //if err := report.ExecuteTemplate(os.Stdout, "temple", res); err != nil { + if err := report.Execute(os.Stdout, res); err != nil { + log.Fatal(err) + } +} diff --git a/src/study/day6/TemplateHTML.go b/src/study/day6/TemplateHTML.go new file mode 100644 index 0000000..41db3ed --- /dev/null +++ b/src/study/day6/TemplateHTML.go @@ -0,0 +1,40 @@ +package main + +import ( + "Study/pkg/github" + "html/template" + "log" + "os" +) + +var IssueList = template.Must(template.New("issuelist").Parse(` +

{{.TotalCount}} issues

+ + + + + + + +{{range .Items}} + + + + + + +{{end}} +
#StateUserTitle
{{.Number}}{{.State}}{{.User.Login}}{{.Title}}
+`)) + +func main() { + res, err := github.SearchIssues([]string{"repo:golang/go", "3133", "10535"}) + if err != nil { + log.Fatal(err) + } + //if err := report.ExecuteTemplate(os.Stdout, "temple", res); err != nil { + if err := IssueList.Execute(os.Stdout, res); err != nil { + log.Fatal(err) + } + +} diff --git a/src/study/day6/TemplateWEB.go b/src/study/day6/TemplateWEB.go new file mode 100644 index 0000000..4d8a9c4 --- /dev/null +++ b/src/study/day6/TemplateWEB.go @@ -0,0 +1,28 @@ +package main + +import ( + "html/template" + "log" + "net/http" +) + +const templ = `

A:{{.A}}

B: {{.B}}

` + +var data struct { + A string + B template.HTML +} + +func main() { + http.HandleFunc("/", handle) + log.Fatal(http.ListenAndServe("0.0.0.0:8989", nil)) +} + +func handle(w http.ResponseWriter, r *http.Request) { + t := template.Must(template.New("escape").Parse(templ)) + data.A = "Hello!" + data.B = "Hello!" + if err := t.Execute(w, data); err != nil { + log.Fatal(err) + } +}