dongzhenju3015 2014-07-10 20:07
浏览 20

将结构传递给Post martini例程

I have an issue using this statement

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

This worked fine if I use the struct define inside the prog like

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

but I need this to be an independent package. I get "Wish is not a type" wish is the return of the binding function. This worked with a primary Type struct. I am passing the strut as a interface{}

I am using GO with Martini.Classic() It is really complicated for me to change Martini or Binding package. Any suggestions.

This is the all code

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"
)

just to show the struct that I need to pass as to routine 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 Returns a 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 returns all Wishes in the database

 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))
   })

binding does not take a pointer. I have to pass the struct by reference on "Wish" the issue is the return on "wish Wish" I got an error Wish is not a type at compilation time

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()

Thanks in advance

Luis

  • 写回答

1条回答 默认 最新

  • duanhui3759 2014-07-11 01:44
    关注

    The reason you are getting an error is that you have called your type Wish1 (with a numerical 1) but you are referring to the Wish type (which does not exist!) in your code.

    Change your struct to be:

    // 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"`
    }
    

    If you want to put your type into another package (tip: don't overdo the sub-packages), then it will need to become a pkgname.Wish as names are fully qualified.

    Added

    After a second look, you're also messing things up here:

     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
    

    Your parameter list needs to provide a name for each type; you can't pass Wish interface{} as a parameter as Wish is a type, not a variable name.

    You should either:

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

    Or, better still, stop using interface{} like this and write:

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

    However, your DoAll function does not seem to be referenced elsewhere, and is creating its own Martini instance. I highly suggest thinking about why things are "split out" like this if you're just starting out. Keep it simple - e.g.

    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: I've fixed the formatting of your code, as it has a lot of unnecessary spacing before/after parenthesis. Make sure to use gofmt, which is included with the Go install and can be hooked into most popular editors.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题