This is a creation in Article, where the information may have evolved or changed.
Controllers and Models
Here we take the news list as an example, do a complete controller processing business, database model to obtain data
Create GetNewsById a way to get a piece of news in the controller
func GetNewsById(ctx dotweb.Context) error { res := model.GetNewsById(ctx) ctx.WriteJson(utils.SuccessReturn(res)) return nil}
Description
ctxContent of the request
ctx.WriteJsonReturn json Format data
utils.SuccessReturnSuccessful return function encapsulated in the toolkit
modelThe introduction of the model directory, the contents are as follows:
// 根据主键id获取一条数据func GetNewsById(ctx dotweb.Context) interface{} { res, err := DB.Table("news"). Where("id", ctx.FormValue("id")). First() if err != nil { return "" } return res}
This is a basic to get the corresponding data according to the parameters, through the import of the database driver, we call the final first method to obtain a data, more usage can refer to the source code
Here, we just take one of the methods to do the example, the other usage is similar, we just need to view the source code, all the source code is in the Https://github.com/gohouse/kuaixinwenz