@@ -7,12 +7,12 @@ import (
7
7
"github.com/kataras/iris/v12/context"
8
8
)
9
9
10
- // User bind struct
10
+ // User example struct for json and msgpack.
11
11
type User struct {
12
- Firstname string `json:"firstname"`
13
- Lastname string `json:"lastname"`
14
- City string `json:"city"`
15
- Age int `json:"age"`
12
+ Firstname string `json:"firstname" msgpack:"firstname" `
13
+ Lastname string `json:"lastname" msgpack:"lastname" `
14
+ City string `json:"city" msgpack:"city" `
15
+ Age int `json:"age" msgpack:"age" `
16
16
}
17
17
18
18
// ExampleXML just a test struct to view represents xml content-type
@@ -22,6 +22,12 @@ type ExampleXML struct {
22
22
Two string `xml:"two,attr"`
23
23
}
24
24
25
+ // ExampleYAML just a test struct to write yaml to the client.
26
+ type ExampleYAML struct {
27
+ Name string `yaml:"name"`
28
+ ServerAddr string `yaml:"ServerAddr"`
29
+ }
30
+
25
31
func main () {
26
32
app := iris .New ()
27
33
@@ -36,15 +42,15 @@ func main() {
36
42
37
43
// Write
38
44
app .Get ("/encode" , func (ctx iris.Context ) {
39
- peter := User {
45
+ u := User {
40
46
Firstname : "John" ,
41
47
Lastname : "Doe" ,
42
48
City : "Neither FBI knows!!!" ,
43
49
Age : 25 ,
44
50
}
45
51
46
52
// Manually setting a content type: ctx.ContentType("application/javascript")
47
- ctx .JSON (peter )
53
+ ctx .JSON (u )
48
54
})
49
55
50
56
// Other content types,
@@ -74,6 +80,21 @@ func main() {
74
80
ctx .Markdown ([]byte ("# Hello Dynamic Markdown -- iris" ))
75
81
})
76
82
83
+ app .Get ("/yaml" , func (ctx iris.Context ) {
84
+ ctx .YAML (ExampleYAML {Name : "Iris" , ServerAddr : "localhost:8080" })
85
+ })
86
+
87
+ app .Get ("/msgpack" , func (ctx iris.Context ) {
88
+ u := User {
89
+ Firstname : "John" ,
90
+ Lastname : "Doe" ,
91
+ City : "Neither FBI knows!!!" ,
92
+ Age : 25 ,
93
+ }
94
+
95
+ ctx .MsgPack (u )
96
+ })
97
+
77
98
// http://localhost:8080/decode
78
99
// http://localhost:8080/encode
79
100
//
@@ -83,6 +104,7 @@ func main() {
83
104
// http://localhost:8080/jsonp
84
105
// http://localhost:8080/xml
85
106
// http://localhost:8080/markdown
107
+ // http://localhost:8080/msgpack
86
108
//
87
109
// `iris.WithOptimizations` is an optional configurator,
88
110
// if passed to the `Run` then it will ensure that the application
0 commit comments