From 4e68a382b78479964b85c683156445b43cb0a0a3 Mon Sep 17 00:00:00 2001 From: bestgopher <84328409@qq.com> Date: Mon, 27 Apr 2020 22:25:53 +0800 Subject: [PATCH] modify:add stringToBytes Former-commit-id: 471bb65ed19ca1fa07f77575e9f11bf0e6fbbec8 --- context/context.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/context/context.go b/context/context.go index e3101c5d..18e57cf5 100644 --- a/context/context.go +++ b/context/context.go @@ -3501,7 +3501,7 @@ func WriteJSON(writer io.Writer, v interface{}, options JSON, optimize bool) (in } if prefix := options.Prefix; prefix != "" { - result = append([]byte(prefix), result...) + result = append(stringToBytes(prefix), result...) } return writer.Write(result) @@ -3512,6 +3512,10 @@ func bytesToString(b []byte) string { return *(*string)(unsafe.Pointer(&b)) } +func stringToBytes(s string) []byte { + return *(*[]byte)(unsafe.Pointer(&s)) +} + // DefaultJSONOptions is the optional settings that are being used // inside `ctx.JSON`. var DefaultJSONOptions = JSON{} @@ -3564,7 +3568,7 @@ var finishCallbackB = []byte(");") // WriteJSONP marshals the given interface object and writes the JSON response to the writer. func WriteJSONP(writer io.Writer, v interface{}, options JSONP, optimize bool) (int, error) { if callback := options.Callback; callback != "" { - n, err := writer.Write([]byte(callback + "(")) + n, err := writer.Write(stringToBytes(callback + "(")) if err != nil { return n, err } @@ -3671,7 +3675,7 @@ func (m xmlMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error { // WriteXML marshals the given interface object and writes the XML response to the writer. func WriteXML(writer io.Writer, v interface{}, options XML, optimize bool) (int, error) { if prefix := options.Prefix; prefix != "" { - n, err := writer.Write([]byte(prefix)) + n, err := writer.Write(stringToBytes(prefix)) if err != nil { return n, err }