ctx.Responsehttp.ResponseWriter.

Text

  1. func text(ctx *clevergo.Context) error {
  2. return ctx.String(http.StatusOK, "hello world")
  3. }

HTML

  1. func html(ctx *clevergo.Context) error {
  2. return ctx.HTML(http.StatusOK, "<html><body>hello world</body></html>")
  3. }
  4. func htmlBlob(ctx *clevergo.Context) error {
  5. return ctx.HTMLBlob(http.StatusOK, []byte("<html><body>hello world</body></html>"))
  6. }

Render

渲染一個模板,詳情參閱視圖

JSON

  1. func json(ctx *clevergo.Context) error {
  2. return ctx.JSON(http.StatusOK, map[string]interface{}{
  3. "message": "hello world",
  4. })
  5. }
  6. func jsonBlob(ctx *clevergo.Context) error {
  7. return ctx.JSONBlob([]byte(`{"message":"hello world"}`))
  8. }

JSONP

  1. func jsonp(ctx *clevergo.Context) error {
  2. return ctx.JSONP(http.StatusOK, map[string]interface{}{
  3. "message": "hello world",
  4. })
  5. }
  6. func jsonpCallback(ctx *clevergo.Context) error {
  7. return ctx.JSONPCallback(http.StatusOK, "myCallback", map[string]interface{}{
  8. "message": "hello world",
  9. })
  10. }

XML

  1. func xml(ctx *clevergo.Context) error {
  2. return ctx.XML(http.StatusOK, map[string]interface{}{
  3. "message": "hello world",
  4. })
  5. }
  6. func xmlBlob(ctx *clevergo.Context) error {
  7. return ctx.XMLBlob(http.StatusOK, []byte(`<xml><message>hello world</message></xml>`))
  8. }

Emit

Emit 發出一個帶有給定狀態碼、內容類型和字符串數據的響應。

  1. func emit(ctx *clevergo.Context) error {
  2. return ctx.Emit(http.StatusOK, "text/html", "data")
  3. }

Blob

Blob 發出一個帶有給定狀態碼、內容類型和字節塊數據的響應。

  1. func blob(ctx *clevergo.Context) error {
  2. return ctx.Blob(http.StatusOK, "text/html", []byte("data"))
  3. }

Redirect

  1. func redirect(ctx *clevergo.Context) error {
  2. ctx.Redirect("/login", http.StatusFound)
  3. return nil
  4. }

SendFile

發送文件讓瀏覽器下載。

  1. func download(ctx *clevergo.Context) error {
  2. buf := bytes.NewReader([]byte("bar"))
  3. return ctx.SendFile("foo.txt", buf)
  4. }

快捷方式

方法
Context.Error http.Error
Context.NotFound http.NotFoud
Context.Redirect http.Redirect
Context.SetCookie http.SetCookie
Context.Write http.ResponseWriter.Write
Context.WriteHeader http.ResponseWriter.WriteHeader