我在使用此语句时遇到问题
m.Post(Model, binding.Form(Wish), func(wish Wish, r render.Render, db *mgo.Database) {如果我在prog中使用struct定义,这可以很好地工作,比如
m.Post(Model, binding.Form(Wish1{}) , func(wish Wish1, r render.Render, db *mgo.Database) {但我需要这是一个独立的包。我得到的"Wish is not a type“wish是绑定函数的返回。这适用于主类型结构。我将strut作为接口进行传递{}
我将GO与Martini.Classic()一起使用,更改马提尼或绑定包对我来说真的很复杂。有什么建议吗。
这是所有的代码
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 is“的返回值。我得到了一个错误:在编译时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()提前感谢
路易斯
发布于 2014-07-11 09:44:04
出现错误的原因是,您调用了类型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安装中,可以连接到大多数流行的编辑器中。
https://stackoverflow.com/questions/24685338
复制相似问题