c.String() performance improvements
``` benchmark old ns/op new ns/op delta BenchmarkOneRouteString 448 310 -30.80% benchmark old allocs new allocs delta BenchmarkOneRouteString 1 0 -100.00% benchmark old bytes new bytes delta BenchmarkOneRouteString 48 0 -100.00% ```
This commit is contained in:
@ -18,14 +18,18 @@ type String struct {
|
||||
var plainContentType = []string{"text/plain; charset=utf-8"}
|
||||
|
||||
func (r String) Render(w http.ResponseWriter) error {
|
||||
WriteString(w, r.Format, r.Data)
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteString(w http.ResponseWriter, format string, data []interface{}) {
|
||||
header := w.Header()
|
||||
if _, exist := header["Content-Type"]; !exist {
|
||||
header["Content-Type"] = plainContentType
|
||||
}
|
||||
if len(r.Data) > 0 {
|
||||
fmt.Fprintf(w, r.Format, r.Data...)
|
||||
if len(data) > 0 {
|
||||
fmt.Fprintf(w, format, data...)
|
||||
} else {
|
||||
io.WriteString(w, r.Format)
|
||||
io.WriteString(w, format)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user