-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_practice.go
54 lines (49 loc) · 1.08 KB
/
json_practice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/labstack/echo"
)
type User struct {
Name string `json:"name" xml:"name" form:"name" query:"name"`
Email string `json:"email" xml:"email" form:"email" query:"email"`
}
type user struct {
Id uint `json:"id"`
Title string `json:"title"`
}
func Theory(c echo.Context) error {
u := new(User)
if err := c.Bind(u); err != nil {
fmt.Printf("error!")
return err
}
return c.JSON(http.StatusOK, u)
// or
// return c.XML(http.StatusCreated, u)
}
// Handler
func GetJson(c echo.Context) error {
u := &User{
Name: "Jon",
Email: "[email protected]",
}
return c.JSON(http.StatusOK, u)
}
func Prettyprint(c echo.Context) error {
u := &User{
Name: "Jon",
Email: "[email protected]",
}
return c.JSONPretty(http.StatusOK, u, " ")
}
func StreamJson(c echo.Context) error {
u := &User{
Name: "Jon",
Email: "[email protected]",
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
return json.NewEncoder(c.Response()).Encode(u)
}