1
0
mirror of https://github.com/labstack/echo.git synced 2025-04-21 12:17:04 +02:00

Removed prinftf from Context#String and Context#HTML, #154

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-11-24 20:03:15 -08:00
parent 754c84596e
commit f54cdd86d0
5 changed files with 19 additions and 30 deletions

View File

@ -6,8 +6,6 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"fmt"
"net/url" "net/url"
"bytes" "bytes"
@ -60,7 +58,7 @@ func (c *Context) Socket() *websocket.Conn {
return c.socket return c.socket
} }
// Path returns the registered path for a handler. // Path returns the registered path for the handler.
func (c *Context) Path() string { func (c *Context) Path() string {
return c.path return c.path
} }
@ -134,31 +132,19 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
return return
} }
// HTML formats according to a format specifier and sends HTML response with // HTML sends an HTTP response with status code.
// status code. func (c *Context) HTML(code int, html string) (err error) {
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
buf := new(bytes.Buffer)
_, err = fmt.Fprintf(buf, format, a...)
if err != nil {
return err
}
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8) c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
c.response.WriteHeader(code) c.response.WriteHeader(code)
c.response.Write(buf.Bytes()) c.response.Write([]byte(html))
return return
} }
// String formats according to a format specifier and sends text response with status // String sends a string response with status code.
// code. func (c *Context) String(code int, s string) (err error) {
func (c *Context) String(code int, format string, a ...interface{}) (err error) {
buf := new(bytes.Buffer)
_, err = fmt.Fprintf(buf, format, a...)
if err != nil {
return err
}
c.response.Header().Set(ContentType, TextPlain) c.response.Header().Set(ContentType, TextPlain)
c.response.WriteHeader(code) c.response.WriteHeader(code)
c.response.Write(buf.Bytes()) c.response.Write([]byte(s))
return return
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"io" "io"
"os" "os"
@ -39,8 +40,8 @@ func upload(c *echo.Context) error {
return err return err
} }
} }
return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.", return c.String(http.StatusOK, fmt.Sprintf("Thank You! %s <%s>, %d files uploaded successfully.",
name, email, len(files)) name, email, len(files)))
} }
func main() { func main() {

View File

@ -1,13 +1,15 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"io" "io"
"net/http" "net/http"
"os" "os"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
) )
func upload(c *echo.Context) error { func upload(c *echo.Context) error {
@ -63,8 +65,8 @@ func upload(c *echo.Context) error {
} }
i++ i++
} }
return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.", return c.String(http.StatusOK, fmt.Sprintf("Thank You! %s <%s>, %d files uploaded successfully.",
name, email, i) name, email, i))
} }
func main() { func main() {

View File

@ -8,7 +8,7 @@ menu:
### Handler path ### Handler path
`Context#Path()` returns the registered path for a handler, it can be used in the `Context#Path()` returns the registered path for the handler, it can be used in the
middleware for logging purpose. middleware for logging purpose.
*Example* *Example*

View File

@ -89,7 +89,7 @@ Sends an XML HTTP response with status code.
Context.HTML(code int, html string) error Context.HTML(code int, html string) error
``` ```
Sends an HTML HTTP response with status code. Sends an HTML response with status code.
### String ### String
@ -97,7 +97,7 @@ Sends an HTML HTTP response with status code.
Context.String(code int, s string) error Context.String(code int, s string) error
``` ```
Sends a text/plain HTTP response with status code. Sends a string response with status code.
### File ### File