模型绑定

自动参数绑定

自动分辨POST OR GET 进行参数绑定

type user struct {
    Name string `json:"name" `
    Password string `json:"password" `
}

func TestBandJson(t *testing.T) {
    app := erguotou.New()

    data := user{}
    app.Post("/testjson", func(ctx *erguotou.Context) {
        value := ctx.BandValue(&data)
        if value != nil {
            panic(value)
        }

        ctx.Json(200,data)
    })

    app.Get("/testjson", func(ctx *erguotou.Context) {
        value := ctx.BandValue(&data)
        if value != nil {
            panic(value)
        }

        log.Println(data)
    })

    err := app.Run(erguotou.SetHost(":8082"))
    if err != nil {
        panic(err)
    }
}

Post参数绑定

    app.Post("/", func(ctx *erguotou.Context) {
        item := item{}
        it := ctx.BindFrom(&item)
        ctx.Json(200, it)
    })

Get参数绑定

    app.Get("/", func(ctx *erguotou.Context) {
        item := item{}
        it := ctx.BindGet(&item)
        ctx.Json(200, it)
    })

results matching ""

    No results matching ""