Beego 框架学习笔记 03 | Put、Delete

Beego 框架学习笔记 03:Put、Delete

一、Put

修改 goods.go:

package controllers

import (
    "github.com/astaxie/beego"
)

type GoodsController struct {  // 首字母要大写
    beego.Controller
}

func (c *GoodsController) Get() {  // get
    c.Data["title"] = "你好beego"
    c.TplName = "goods.tpl"
}

func (c *GoodsController) DoAdd() {  // post
    c.Ctx.WriteString("执行增加操作")
}

func (c *GoodsController) DoEdit() {  // put
    c.Ctx.WriteString("执行修改操作")
}

func (c *GoodsController) DoDelete() {  // delete
    c.Ctx.WriteString("执行删除操作")
}

在 router.go 中添加路由:

package routers

import (
    "beegodemo/controllers"
    "github.com/astaxie/beego"
)

func init() {
    beego.Router("/", &controllers.MainController{})

    beego.Router("/article", &controllers.ArticleController{})  // 访问 Get 方法
    beego.Router("/article/add", &controllers.ArticleController{}, "get:AddArticle")  // 访问自定义方法
    beego.Router("/article/edit", &controllers.ArticleController{}, "get:EditArticle")  // 结构体地址

    beego.Router("/user", &controllers.UserController{})
    beego.Router("/user/add", &controllers.UserController{}, "get:AddUser")  // 结构体地址
    beego.Router("/user/doAdd", &controllers.UserController{}, "post:DoAddUser")
    beego.Router("/user/edit", &controllers.UserController{}, "get:EditUser")  // 结构体地址
    beego.Router("/user/doEdit", &controllers.UserController{}, "post:DoEditUser")
    beego.Router("/user/getUser", &controllers.UserController{}, "get:GetUser")  // 首字母大写

    beego.Router("/goods", &controllers.GoodsController{})
    beego.Router("/goods/add", &controllers.GoodsController{}, "post:DoAdd")
    beego.Router("/goods/edit", &controllers.GoodsController{}, "put:DoEdit")
    beego.Router("/goods/delete", &controllers.GoodsController{}, "delete:DoDelete")

}

使用 Postman 来测试接口:

二、Delete

goods.go:

package controllers

import (
    "github.com/astaxie/beego"
    "strconv"
)

type GoodsController struct {  // 首字母要大写
    beego.Controller
}

func (c *GoodsController) Get() {  // get
    c.Data["title"] = "你好beego"
    c.TplName = "goods.tpl"
}

func (c *GoodsController) DoAdd() {  // post
    c.Ctx.WriteString("执行增加操作")
}

func (c *GoodsController) DoEdit() {  // put
    title := c.GetString("title")
    c.Ctx.WriteString("执行修改操作--" + title)
}

func (c *GoodsController) DoDelete() {  // delete
    id, err := c.GetInt("id")
    if err != nil {
        c.Ctx.WriteString("参数错误")
    }
    c.Ctx.WriteString("执行删除操作--" + strconv.Itoa(id))
}

router.go:

package routers

import (
    "beegodemo/controllers"
    "github.com/astaxie/beego"
)

func init() {
    beego.Router("/", &controllers.MainController{})

    beego.Router("/article", &controllers.ArticleController{})  // 访问 Get 方法
    beego.Router("/article/add", &controllers.ArticleController{}, "get:AddArticle")  // 访问自定义方法
    beego.Router("/article/edit", &controllers.ArticleController{}, "get:EditArticle")  // 结构体地址

    beego.Router("/user", &controllers.UserController{})
    beego.Router("/user/add", &controllers.UserController{}, "get:AddUser")  // 结构体地址
    beego.Router("/user/doAdd", &controllers.UserController{}, "post:DoAddUser")
    beego.Router("/user/edit", &controllers.UserController{}, "get:EditUser")  // 结构体地址
    beego.Router("/user/doEdit", &controllers.UserController{}, "post:DoEditUser")
    beego.Router("/user/getUser", &controllers.UserController{}, "get:GetUser")  // 首字母大写

    beego.Router("/goods", &controllers.GoodsController{})
    beego.Router("/goods/add", &controllers.GoodsController{}, "post:DoAdd")
    beego.Router("/goods/edit", &controllers.GoodsController{}, "put:DoEdit")
    beego.Router("/goods/delete", &controllers.GoodsController{}, "delete:DoDelete")

}

在 Postman 中在 Body 中输入 Key 和 Value,点击 Send,可以看到:

同样的,edit 也是:

图片说明

三、参考教程

Golang 教程 P54

Go学习笔记 文章被收录于专栏

自己学习 Go 语言记录的笔记。

全部评论

相关推荐

10-25 12:05
已编辑
湖南科技大学 Java
若梦难了:我有你这简历,已经大厂乱杀了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务