duanguan5922 2014-11-12 09:14
浏览 37
已采纳

狂欢:“代码无法编译:未定义:模型”

I have created /app/models/todo-item.go file that looks like this:

package models

import (
  "github.com/revel/revel"
)

type TodoItem struct {
  Id              int64   `db:"id" json:"id"`
  Name            string  `db:"name" json:"name"`
}

func (b *TodoItem) Validate(v *revel.Validation) {

  v.Check(b.Name,
    revel.ValidRequired(),
    revel.ValidMaxSize(25))
}

In src/RevelApp/app/controllers/init.go, I have this (PS, I am using GorpController to interact with MySQL):

func defineTodoItemTable(dbm *gorp.DbMap){
  // set "id" as primary key and autoincrement
  t := dbm.AddTable(models.TodoItem{}).SetKeys(true, "id") 
  t.ColMap("name").SetMaxSize(25)
}

I am getting an error : The Go code src/RevelApp/app/controllers/init.go does not compile: undefined: models

I have tried importing ."RevelApp/app/models" then doing away with models in models.TodoItem{} (as describer here: Revel with Gorm "undefined: Page") and I get the error : App failed to start up revel/harness: app timed out.

That link is the only one I could find related to this issue. Am I missing something?

EDIT: $GOPATH:

/home/me/Source/go

models location:

/home/me/Source/go/src/RevelApp/app/models

How I am importing models package:

import (
    ."RevelApp/app/models"
    "github.com/revel/revel"
    "github.com/coopernurse/gorp"
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
    "fmt"
    "strings"
)
  • 写回答

1条回答 默认 最新

  • doumu1873 2014-11-12 10:44
    关注

    You have imported your models using the dot (.) import qualifier. From the language spec:

    If an explicit period (.) appears instead of a name, all the package's exported identifiers will be declared in the current file's file block and can be accessed without a qualifier.

    This means you don't need to use models.TodoItem .. you can simply use TodoItem.

    That said .. I would suggest avoiding this and removing the dot from the import statement:

    import (
        "RevelApp/app/models"
        "github.com/revel/revel"
        "github.com/coopernurse/gorp"
        "database/sql"
        _ "github.com/go-sql-driver/mysql"
        "fmt"
        "strings"
    )
    

    Why? It stops your local file scope becoming crowded and allows you to see where each object actually resides.

    For completeness (and you're probably wondering what its for since the dot is similar), that underscore (_) before the mysql package statement basically says "import this package .. but I don't need to use anything in it directly". This fires the packages init function allowing it to register itself with the database/sql packages routines. Which is why when you use the sql.DB type it is eventually routed to the MySql package code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。