本文整理汇总了Golang中code/google/com/p/uedatakuya-goweb/goweb.Context.RespondWithData方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.RespondWithData方法的具体用法?Golang Context.RespondWithData怎么用?Golang Context.RespondWithData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类code/google/com/p/uedatakuya-goweb/goweb.Context
的用法示例。
在下文中一共展示了Context.RespondWithData方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Read
func (cr *Controller) Read(id string, cx *goweb.Context) {
log.Printf("Read a diary id=%s", id)
c := cr.db.C(COLLECTION)
var diary Diary
if err := c.FindId(id).One(&diary); err != nil {
log.Println("Error: %s", err.Error())
cx.RespondWithError(http.StatusForbidden)
return
}
log.Printf("Read diary id=%s", id)
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
cx.RespondWithData(diary)
}
示例2: Create
func (cr *Controller) Create(cx *goweb.Context) {
log.Println("Creating a diary...")
c := cr.db.C(COLLECTION)
var diary Diary
decoder := new(goweb.JsonRequestDecoder)
decoder.Unmarshal(cx, &diary)
diary.Id = bson.NewObjectId().Hex()
if err := c.Insert(&diary); err != nil {
log.Println("Error: %s", err.Error())
cx.RespondWithError(http.StatusForbidden)
return
}
log.Printf("Created diary id=%s", diary.Id)
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
cx.RespondWithData(diary.Id)
}
示例3: ReadMany
func (cr *Controller) ReadMany(cx *goweb.Context) {
log.Println("Read all diaries...")
c := cr.db.C(COLLECTION)
count, err := c.Count()
if err != nil {
log.Println("Error: %s", err.Error())
cx.RespondWithError(http.StatusForbidden)
return
}
diaries := make([]*Diary, count)
if err := c.Find(nil).All(&diaries); err != nil {
log.Println("Error: %s", err.Error())
cx.RespondWithError(http.StatusForbidden)
return
}
log.Printf("Read all %d diaries", count)
cx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
cx.RespondWithData(diaries)
}