【问题标题】:Passing an struct to a Post martini routine将结构传递给 Post martini 例程
【发布时间】:2014-07-10 20:07:15
【问题描述】:

我在使用此语句时遇到问题

  m.Post(Model, binding.Form(Wish), func(wish Wish, r render.Render, db *mgo.Database) {

如果我在 prog 中使用结构定义,则效果很好

  m.Post(Model, binding.Form(Wish1{}) , func(wish Wish1, r render.Render, db     *mgo.Database) {

但我需要这是一个独立的包。 我得到“希望不是类型”希望是绑定函数的返回。 这适用于主要类型结构。我将支柱作为接口传递{}

我正在使用 GO 和 Martini.Classic() 更改 Martini 或 Binding 包对我来说真的很复杂。任何建议。

这是全部代码

package chlistpkg
import (
  "github.com/codegangsta/martini"
  "github.com/codegangsta/martini-contrib/binding"
  "github.com/codegangsta/martini-contrib/render"
  "labix.org/v2/mgo"
  "time"
  "fmt"
  "html/template"
  "reflect"
  "adminStruct"
)

只是为了显示我需要传递给例程 Doall 的结构

type Wish1 struct {
  Name        string `form:"name"`
  Description string `form:"description"`
  AnyDate     time.Time  `form:"anydate"`
  Active      bool   `form:"active"`
  Number      int    `form:"number"`
  NumDec      float32  `form:"numDec"`
 }

DB 返回一个 martini.Handler

 func DB() martini.Handler {
    session, err := mgo.Dial("mongodb://localhost")
    if err != nil {
      panic(err)
     }

 return func(c martini.Context) {
    s := session.Clone()
    c.Map(s.DB("advent2"))
    defer s.Close()
    c.Next()
}

}

GetAll 返回数据库中的所有愿望

 func GetAll(db *mgo.Database, entList interface{}) interface{} {
   db.C("wishes").Find(nil).All(entList)
   fmt.Println("GettAll entList =", entList)
   return entList
 }


   func Doall(Model string, Wish interface{}, Wish2 interface{}, Wishlist interface{}         ) {
     m := martini.Classic()
     fmt.Println ("martini.Classic =", m)
     m.Use(martini.Static("images")) // serve from the "images" directory as well

     m.Use(render.Renderer(render.Options{
        Directory: "templates",
        Layout: "layout",
      }))

   m.Use(DB())

   m.Get(Model, func(r render.Render, db *mgo.Database) {
     r.HTML(200, "lista4", GetAll(db,  Wishlist))
   })

绑定不需要指针。我必须通过“Wish”上的引用来传递结构 问题是返回“wish Wish”我收到一个错误 Wish 不是类型 在编译时

m.Post(Model, binding.Form(Wish), func(wish Wish, r render.Render, db *mgo.Database) {
fmt.Println("Input wish =", wish)
db.C("wishes").Insert(wish)
r.HTML(200, "lista4", GetAll(db, Wishlist))
})

m.Run()

提前致谢

路易斯

【问题讨论】:

  • 你能发布错误所指的行吗?你的代码有很多问题。

标签: data-binding go martini


【解决方案1】:

您收到错误的原因是您调用了您的类型 Wish1(带有数字 1),但您在代码中指的是 Wish 类型(不存在!)。

将您的结构更改为:

// Note: "Wish", not "Wish1"
type Wish struct {
  Name        string `form:"name"`
  Description string `form:"description"`
  AnyDate     time.Time  `form:"anydate"`
  Active      bool   `form:"active"`
  Number      int    `form:"number"`
  NumDec      float32  `form:"numDec"`
}

如果您想将您的类型放入另一个包中(提示:不要过度使用子包),那么它需要成为 pkgname.Wish,因为名称是完全限定的。

已添加

再看一遍,你也把事情搞砸了:

 func Doall(Model string, Wish interface{}, Wish2 interface{}, Wishlist interface{}         ) {
     m := martini.Classic()
     fmt.Println ("martini.Classic =", m)
     m.Use(martini.Static("images")) // serve from the "images" directory as well

您的参数列表需要为每种类型提供一个名称;您不能将Wish interface{} 作为参数传递,因为Wish 是一种类型,而不是变量名。

您应该:

func DoAll(model string, wish interface{}, wish2 interface{}, wishList interface{}) { ... }

或者,更好的是,停止像这样使用interface{} 并写:

func DoAll(model string, wishList []Wish, wishes... Wish) { ... }

但是,您的 DoAll 函数似乎没有在其他地方被引用,并且正在创建自己的 Martini 实例。如果你刚刚开始,我强烈建议你考虑一下为什么事情会像这样“分裂”。保持简单 - 例如

func main() {
    m := martini.Classic()
    m.Use(martini.Static("images"))
    m.Use(DB())
    m.Use(render.Renderer(render.Options{...}))
    // No need for an anonymous function, which just adds clutter
    m.Get("/wishes/all", GetAllWishes)
    // Same goes for here
    m.Post("/wishes/new", PostWish)

    m.Run()
}

PS:我已经修复了代码的格式,因为括号前后有很多不必要的空格。确保使用 gofmt,它包含在 Go 安装中,可以连接到大多数流行的编辑器中。

【讨论】:

  • 好吧,看起来将类型结构放入包中的唯一方法是使用您上面提到的“pkgname.Wish”。感谢您的回答。
猜你喜欢
  • 2015-12-24
  • 1970-01-01
  • 2012-07-06
  • 2023-03-15
  • 2019-09-19
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
相关资源
最近更新 更多