新聞中心
從v1.15版本開始,?Request?請求對象支持通過?struct tag?的方式為輸入對象的屬性綁定默認(rèn)值。默認(rèn)值的?struct tag?名稱為?d?(也可以使用?default?)。

成都創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站建設(shè)、網(wǎng)站制作與策劃設(shè)計,遷安網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:遷安等地區(qū)。遷安做網(wǎng)站價格咨詢:028-86922220
我們來看一個示例以便更好理解。
參數(shù)對象定義
type ContentServiceGetListReq struct {
Type string // 內(nèi)容模型
CateGOryId uint `p:"cate"` // 欄目ID
Page int `d:"1" v:"min:0#分頁號碼錯誤"` // 分頁號碼
Size int `d:"10" v:"max:50#分頁數(shù)量最大50條"` // 分頁數(shù)量,最大50
Sort int // 排序類型(0:最新, 默認(rèn)。1:活躍, 2:熱度)
}這個是一個查詢內(nèi)容列表請求的參數(shù)接受對象,其中我們通過?d?的標(biāo)簽為屬性?Page?和?Size?指定了默認(rèn)值,當(dāng)這兩個參數(shù)不傳遞時,默認(rèn)為1和10,表示分頁從第1頁開始,每頁查詢數(shù)量為10。
參數(shù)對象使用
// @summary 展示文章首頁
// @tags 前臺-文章
// @produce html
// @param cate query int false "欄目ID"
// @param page query int false "分頁號碼"
// @param size query int false "分頁數(shù)量"
// @param sort query string false "排序方式"
// @router /article [GET]
// @success 200 {string} html "頁面HTML"
func (a *articleApi) Index(r *ghttp.Request) {
var (
data *define.ContentServiceGetListReq
)
if err := r.Parse(&data); err != nil {
service.View.Render500(r, model.View{
Error: err.Error(),
})
}
data.Type = model.ContentTypeArticle
if getListRes, err := service.Content.GetList(r.Context(), data); err != nil {
service.View.Render500(r, model.View{
Error: err.Error(),
})
} else {
service.View.Render(r, model.View{
ContentType: data.Type,
Data: getListRes,
Title: service.View.GetTitle(r.Context(), &define.ViewServiceGetTitleReq{
ContentType: data.Type,
CategoryId: data.CategoryId,
}),
})
}
}這個一個?MVC?設(shè)計模式中的一個文章查詢接口,該接口負(fù)責(zé)查詢內(nèi)容列表的請求并渲染展示文章列表頁面。可以看到這里使用了?Parse?方法直接獲取并轉(zhuǎn)換客戶端提交的參數(shù)到?ContentServiceGetListReq?對象上。當(dāng)然,這里也可以使用?GetStruct?方法執(zhí)行參數(shù)獲取和對象初始化。
網(wǎng)頁題目:創(chuàng)新互聯(lián)GoFrame教程:GoFrame請求輸入-默認(rèn)值綁定
網(wǎng)頁地址:http://fisionsoft.com.cn/article/djjopjp.html


咨詢
建站咨詢
