2
0

Avoid using fmt.Fprint()

This commit is contained in:
Manu Mtz-Almeida
2015-05-19 00:02:44 +02:00
parent 320b4a6cca
commit c574f6c50c
+13 -12
View File
@@ -22,16 +22,6 @@ type Event struct {
Data interface{}
}
func (r Event) Write(w http.ResponseWriter) error {
header := w.Header()
header.Set("Content-Type", ContentType)
if _, exist := header["Cache-Control"]; !exist {
header.Set("Cache-Control", "no-cache")
}
return Encode(w, r)
}
func Encode(w io.Writer, event Event) error {
writeId(w, event.Id)
writeEvent(w, event.Event)
@@ -72,11 +62,22 @@ func writeData(w io.Writer, data interface{}) error {
w.Write([]byte("\n"))
default:
text := fmt.Sprint(data)
fmt.Fprint(w, escape(text), "\n\n")
w.Write([]byte(escape(text)))
w.Write([]byte("\n\n"))
}
return nil
}
func (r Event) Write(w http.ResponseWriter) error {
header := w.Header()
header.Set("Content-Type", ContentType)
if _, exist := header["Cache-Control"]; !exist {
header.Set("Cache-Control", "no-cache")
}
return Encode(w, r)
}
func typeOfData(data interface{}) reflect.Kind {
value := reflect.ValueOf(data)
valueType := value.Kind()
@@ -88,7 +89,7 @@ func typeOfData(data interface{}) reflect.Kind {
func escape(str string) string {
// any-char = %x0000-0009 / %x000B-000C / %x000E-10FFFF
// ; a Unicode character other than U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR)
// ; a Unicode character other than U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR)
str = strings.Replace(str, "\n", "\\n", -1)
str = strings.Replace(str, "\r", "\\r", -1)
return str