From a2105ce34c7883066281249f5efda0c4390853ea Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sat, 23 May 2015 16:39:25 +0200 Subject: [PATCH] Fast path for setting headers. - No heap allocation - No indirection - CanonicalMIMEHeaderKey() is not called --- render/html.go | 4 ++-- render/json.go | 6 +++--- render/text.go | 4 ++-- render/xml.go | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/render/html.go b/render/html.go index 921c070..ba9f01a 100644 --- a/render/html.go +++ b/render/html.go @@ -30,7 +30,7 @@ type ( } ) -const htmlContentType = "text/html; charset=utf-8" +var htmlContentType = []string{"text/html; charset=utf-8"} func (r HTMLProduction) Instance(name string, data interface{}) Render { return HTML{ @@ -58,6 +58,6 @@ func (r HTMLDebug) loadTemplate() *template.Template { } func (r HTML) Write(w http.ResponseWriter) error { - w.Header().Set("Content-Type", htmlContentType) + w.Header()["Content-Type"] = htmlContentType return r.Template.ExecuteTemplate(w, r.Name, r.Data) } diff --git a/render/json.go b/render/json.go index 06d8606..2fc4dfd 100644 --- a/render/json.go +++ b/render/json.go @@ -19,15 +19,15 @@ type ( } ) -const jsonContentType = "application/json; charset=utf-8" +var jsonContentType = []string{"application/json; charset=utf-8"} func (r JSON) Write(w http.ResponseWriter) error { - w.Header().Set("Content-Type", jsonContentType) + w.Header()["Content-Type"] = jsonContentType return json.NewEncoder(w).Encode(r.Data) } func (r IndentedJSON) Write(w http.ResponseWriter) error { - w.Header().Set("Content-Type", jsonContentType) + w.Header()["Content-Type"] = jsonContentType jsonBytes, err := json.MarshalIndent(r.Data, "", " ") if err != nil { return err diff --git a/render/text.go b/render/text.go index 1135358..d1e901f 100644 --- a/render/text.go +++ b/render/text.go @@ -14,12 +14,12 @@ type String struct { Data []interface{} } -const plainContentType = "text/plain; charset=utf-8" +var plainContentType = []string{"text/plain; charset=utf-8"} func (r String) Write(w http.ResponseWriter) error { header := w.Header() if _, exist := header["Content-Type"]; !exist { - header.Set("Content-Type", plainContentType) + header["Content-Type"] = plainContentType } if len(r.Data) > 0 { fmt.Fprintf(w, r.Format, r.Data...) diff --git a/render/xml.go b/render/xml.go index 8061d49..58eeecc 100644 --- a/render/xml.go +++ b/render/xml.go @@ -13,9 +13,9 @@ type XML struct { Data interface{} } -const xmlContentType = "application/xml; charset=utf-8" +var xmlContentType = []string{"application/xml; charset=utf-8"} func (r XML) Write(w http.ResponseWriter) error { - w.Header().Set("Content-Type", xmlContentType) + w.Header()["Content-Type"] = xmlContentType return xml.NewEncoder(w).Encode(r.Data) }