我们可以向Beego路由器传递额外的参数吗?

huangapple go评论122阅读模式
英文:

can we pass extra parameter to a beego router

问题

你好,以下是翻译好的内容:

  1. type MainController struct {
  2. beego.Controller
  3. }
  4. func (this *MainController) Post() {
  5. var datapoint User
  6. req := this.Ctx.Input.RequestBody
  7. json.Unmarshal([]byte(req), &datapoint)
  8. this.Ctx.WriteString("hello world")
  9. // result := this.Input()
  10. fmt.Println("input value is", datapoint.UserId)
  11. }

这是一个正常的beego路由器,它在URL出现时执行。我想要像下面这样的东西:

  1. type MainController struct {
  2. beego.Controller
  3. }
  4. func (this *MainController, db *sql.DB) Post() {
  5. fmt.Println("input value is", datapoint.UserId)
  6. }

以便使用数据库连接指针。请问在Go语言中是否可以实现这个?如果不行,请给予建议。

英文:
  1. type MainController struct {
  2. beego.Controller
  3. }
  4. func (this *MainController) Post() {
  5. var datapoint User
  6. req := this.Ctx.Input.RequestBody
  7. json.Unmarshal([]byte(req), &datapoint)
  8. this.Ctx.WriteString("hello world")
  9. // result := this.Input()
  10. fmt.Println("input value is", datapoint.UserId)
  11. }

this is normal beego router which executes on url occurrence. I want something like

  1. type MainController struct {
  2. beego.Controller
  3. }
  4. func (this *MainController,db *sql.DB) Post() {
  5. fmt.Println("input value is", datapoint.UserId)
  6. }

in order to use database connection pointer. is this possible to achieve this using go...if not please suggest

答案1

得分: 0

你不能在golang中这样做。

  1. type MainController struct {
  2. beego.Controller
  3. }
  4. func (this *MainController, db *sql.DB) Post() {
  5. fmt.Println("input value is", datapoint.UserId)
  6. }

在这个形式的声明中,

  1. function (c *MainController)Post()

意味着PostMainController结构体的一个方法,你可以将变量传递给它。

我的建议是你在包中将db指针声明为全局变量,如下所示,然后你就可以在Post函数内部使用它了。

  1. type MainController struct {
  2. beego.Controller
  3. }
  4. var db *sql.DB
  5. func (this *MainController) Post() {
  6. // 现在你可以在这里使用变量db了
  7. fmt.Println("input value is", datapoint.UserId)
  8. }

但考虑到这是使用MVC模式的Beego框架,你可以在模型中完成所有这些操作。我建议你查看他们很棒的文档

英文:

You can't do this in golang

  1. type MainController struct {
  2. beego.Controller }
  3. func (this *MainController,db *sql.DB) Post() {
  4. fmt.Println("input value is", datapoint.UserId)
  5. }

the declaration in the form

  1. function (c *MainController)Post()

means that Post is a method for the MainController struct you can pass variable into it.

My suggestion is you declare the db pointer as a global variable in your package like below then you will be able to use it inside the Post function

  1. type MainController struct {
  2. beego.Controller
  3. }
  4. var db *sql.DB
  5. func (this *MainController) Post() {
  6. //you can now use the variable db inside here
  7. fmt.Println("input value is", datapoint.UserId)
  8. }

but considering this is Beego which uses the MVC pattern you can do all this from within the model. I suggest you take a look at their awesome documentation here

huangapple
  • 本文由 发表于 2017年2月27日 14:26:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/42479117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定