[日常] Go语言圣经-文本和HTML模板习题

Go语言圣经-文本和HTML模板

练习 4.14: 创建一个web服务器,查询一次GitHub,然后生成BUG报告、里程碑和对应的用户信息。

1.查看下文档godoc net/http |grep HandleFunc

func HandleFunc(pattern string, handler func(ResponseWriter, *Request))

2.godoc fmt|grep Fprintf

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

3. var issueList = template.Must(template.New("issuelist").Parse(``)

issueList.Execute(w, result)

 

package main

import (
        "net/http"
        //"fmt"
        "github"
        "html/template"
)

func main() {
        //启动一个web服务器
        http.HandleFunc("/", handle)
        http.ListenAndServe("0.0.0.0:8000", nil)
}

func handle(w http.ResponseWriter, r *http.Request) {
        var result *github.IssuesSearchResult
        var keywords = []string{"php", "js"}
        result, _ = github.SearchIssues(keywords)

        var issueList = template.Must(template.New("issuelist").Parse(`
<h1>{{.TotalCount}} issues</h1>
<table>
<tr style='text-align: left'>
  <th>#</th>
  <th>State</th>
  <th>User</th>
  <th>Title</th>
</tr>
{{range .Items}}
<tr>
  <td><a href='{{.HTMLURL}}'>{{.Number}}</a></td>
  <td>{{.State}}</td>
  <td><a href='{{.User.HTMLURL}}'>{{.User.Login}}</a></td>
  <td><a href='{{.HTMLURL}}'>{{.Title}}</a></td>
</tr>
{{end}}
</table>
`))
        issueList.Execute(w, result)
        //fmt.Fprintf(w,"hello world")
}

  

posted @ 2018-04-13 14:12  唯一客服系统开发笔记  阅读(599)  评论(0编辑  收藏  举报