गो की मेरी पसंदीदा विशेषताओं में से एक पैकेज के अंदर आसानी से हैंडलर जोड़ने की क्षमता है। यह मॉड्यूलर कोड लिखने की प्रक्रिया को बहुत सरल बनाता है।
उदाहरण के लिए:
फ़ाइल संरचना
|-- 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 उपसर्ग के स्वामित्व लेता है।उदाहरण के लिए पैकेज github.com/storeski/appengine/products
के तहत /products
से शुरू होने वाले किसी भी इनबाउंड यूआरएल का मालिक होगा।
इस मॉड्यूलर दृष्टिकोण के साथ products
पैकेज के भीतर टेम्पलेट्स को स्टोर करना फायदेमंद है। यदि आप साइट के लिए एक सतत आधार टेम्पलेट को बनाए रखना चाहते हैं तो आप एक सम्मेलन स्थापित कर सकते हैं जहां आप templates/base.html
का विस्तार करते हैं।
उदाहरण
टेम्पलेट्स/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/pro नलिकाओं/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)
}
}
क्योंकि इसमें ऐप्स/पैकेज तुच्छ के आदान-प्रदान करता है यह दृष्टिकोण बहुत ही रोमांचक है। अगर मैं एक पैकेज लिखता हूं जो प्रमाणीकरण को संभालता है जो /auth
यूआरएल का स्वामित्व लेता है। कोई भी डेवलपर, जो, पैकेज को उनके उत्पाद रूट में जोड़ता है, तुरंत सभी कार्यक्षमता है। उन्हें बस एक बेस टेम्पलेट (templates/base.html
) बनाना है और अपने उपयोगकर्ताओं को /auth
पर निर्देशित करना है।
स्रोत
2012-03-06 16:18:42
अच्छा लेआउट, केली। सामग्री टेम्पलेट सहित, आपको शायद डॉट (।) जोड़ना चाहिए, उदा। '{{टेम्पलेट" सामग्री "।}}'। अन्यथा, आपका टीसी उत्पादों/टेम्पलेट्स/list.html – alex
में उपलब्ध नहीं होगा धन्यवाद @alex - मैंने आपके सुझाव को शामिल करने के लिए उत्तर संपादित किया है। –
बहुत अच्छा, केली। आप इस लेआउट का उपयोग कर मॉडल को संभालने का सुझाव कैसे देते हैं? मेरा मतलब http.go के अंदर के प्रकारों को घोषित करना या शायद model.go फ़ाइल बनाना है? अग्रिम में धन्यवाद। –