duanguan5922 2014-11-12 01: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 02: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 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口