mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 02:47:04 +00:00
Add More Examples & Categorized in Folders & TOC
Former-commit-id: ce4d711a75a4ba08ffab075e6baa88724725885b
This commit is contained in:
37
_examples/examples/beginner/write-json/main.go
Normal file
37
_examples/examples/beginner/write-json/main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
// User bind struct
|
||||
type User struct {
|
||||
Firstname string `json:"firstname"`
|
||||
Lastname string `json:"lastname"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Adapt(httprouter.New())
|
||||
|
||||
app.Post("/decode", func(ctx *iris.Context) {
|
||||
var user User
|
||||
ctx.ReadJSON(&user)
|
||||
|
||||
ctx.Writef("%s %s is %d years old!", user.Firstname, user.Lastname, user.Age)
|
||||
})
|
||||
|
||||
app.Get("/encode", func(ctx *iris.Context) {
|
||||
peter := User{
|
||||
Firstname: "John",
|
||||
Lastname: "Doe",
|
||||
Age: 25,
|
||||
}
|
||||
|
||||
ctx.JSON(iris.StatusOK, peter)
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
Reference in New Issue
Block a user