Skip to content

Commit f54cdd8

Browse files
committed
Removed prinftf from Context#String and Context#HTML, #154
Signed-off-by: Vishal Rana <[email protected]>
1 parent 754c845 commit f54cdd8

File tree

5 files changed

+19
-30
lines changed

5 files changed

+19
-30
lines changed

context.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"net/http"
77
"path/filepath"
88

9-
"fmt"
10-
119
"net/url"
1210

1311
"bytes"
@@ -60,7 +58,7 @@ func (c *Context) Socket() *websocket.Conn {
6058
return c.socket
6159
}
6260

63-
// Path returns the registered path for a handler.
61+
// Path returns the registered path for the handler.
6462
func (c *Context) Path() string {
6563
return c.path
6664
}
@@ -134,31 +132,19 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
134132
return
135133
}
136134

137-
// HTML formats according to a format specifier and sends HTML response with
138-
// status code.
139-
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
140-
buf := new(bytes.Buffer)
141-
_, err = fmt.Fprintf(buf, format, a...)
142-
if err != nil {
143-
return err
144-
}
135+
// HTML sends an HTTP response with status code.
136+
func (c *Context) HTML(code int, html string) (err error) {
145137
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
146138
c.response.WriteHeader(code)
147-
c.response.Write(buf.Bytes())
139+
c.response.Write([]byte(html))
148140
return
149141
}
150142

151-
// String formats according to a format specifier and sends text response with status
152-
// code.
153-
func (c *Context) String(code int, format string, a ...interface{}) (err error) {
154-
buf := new(bytes.Buffer)
155-
_, err = fmt.Fprintf(buf, format, a...)
156-
if err != nil {
157-
return err
158-
}
143+
// String sends a string response with status code.
144+
func (c *Context) String(code int, s string) (err error) {
159145
c.response.Header().Set(ContentType, TextPlain)
160146
c.response.WriteHeader(code)
161-
c.response.Write(buf.Bytes())
147+
c.response.Write([]byte(s))
162148
return
163149
}
164150

recipes/file-upload/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"io"
56
"os"
67

@@ -39,8 +40,8 @@ func upload(c *echo.Context) error {
3940
return err
4041
}
4142
}
42-
return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.",
43-
name, email, len(files))
43+
return c.String(http.StatusOK, fmt.Sprintf("Thank You! %s <%s>, %d files uploaded successfully.",
44+
name, email, len(files)))
4445
}
4546

4647
func main() {

recipes/streaming-file-upload/server.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package main
22

33
import (
4+
"fmt"
45
"io/ioutil"
56

6-
"github.com/labstack/echo"
7-
mw "github.com/labstack/echo/middleware"
87
"io"
98
"net/http"
109
"os"
10+
11+
"github.com/labstack/echo"
12+
mw "github.com/labstack/echo/middleware"
1113
)
1214

1315
func upload(c *echo.Context) error {
@@ -63,8 +65,8 @@ func upload(c *echo.Context) error {
6365
}
6466
i++
6567
}
66-
return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.",
67-
name, email, i)
68+
return c.String(http.StatusOK, fmt.Sprintf("Thank You! %s <%s>, %d files uploaded successfully.",
69+
name, email, i))
6870
}
6971

7072
func main() {

website/content/guide/request.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ menu:
88

99
### Handler path
1010

11-
`Context#Path()` returns the registered path for a handler, it can be used in the
11+
`Context#Path()` returns the registered path for the handler, it can be used in the
1212
middleware for logging purpose.
1313

1414
*Example*

website/content/guide/response.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ Sends an XML HTTP response with status code.
8989
Context.HTML(code int, html string) error
9090
```
9191

92-
Sends an HTML HTTP response with status code.
92+
Sends an HTML response with status code.
9393

9494
### String
9595

9696
```go
9797
Context.String(code int, s string) error
9898
```
9999

100-
Sends a text/plain HTTP response with status code.
100+
Sends a string response with status code.
101101

102102
### File
103103

0 commit comments

Comments
 (0)