我最喜欢的Go功能之一就是能够轻松地在包内添加处理程序。这大大简化了编写模块化代码的过程。
例如:
档案结构
|-- app.yaml|-- app| +-- http.go|-- templates| +-- base.html+-- github.com +-- storeski +-- appengine |-- products | |-- http.go | +-- templates | |-- list.html | +-- detail.html +-- account |-- http.go +-- templates |-- overview.html +-- notifications.html
每个软件包都有一个http.go文件,该文件拥有url前缀的所有权。例如,
products下面的包
github.com/storeski/appengine/products将拥有任何以开头的入站URL
/products。
使用这种模块化方法,将模板存储在
products包中是有益的。如果您希望为站点维护一致的基本模板,则可以在扩展位置建立约定
templates/base.html。
例
templates / base.html
<!DOCTYPE HTML><html> <head> <title>{{.Store.Title}}</title> </head> <body> <div id="content"> {{template "content" .}} </div> </body></html>_github.com/storeski/appengine/products/templates/list.html_
{{define "content"}} <h1> Products List </h1>{{end}}_github.com/storeski/appengine/products/http.go_
func init() { http.HandleFunc("/products", listHandler)}var listTmpl = template.Must(template.ParseFiles("templates/base.html", "github.com/storeski/appengine/products/templates/list.html"))func listHandler(w http.ResponseWriter, r *http.Request) { tc := make(map[string]interface{}) tc["Store"] = Store tc["Products"] = Products if err := listTmpl.Execute(w, tc); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) }}这种方法非常令人兴奋,因为它使应用程序/软件包的共享变得微不足道。如果我编写了一个处理身份验证的程序包,则该程序包获取
/authURL的所有权。然后,任何将软件包立即添加到其产品根目录的开发人员都具有所有功能。他们要做的就是创建一个基本模板(
templates/base.html)并将其用户定向到
/auth。



