Revel: “代码无法编译:未定义:models”

huangapple go评论109阅读模式
英文:

Revel: "code does not compile: undefined: models"

问题

我已经创建了一个名为<app-root>/app/models/todo-item.go的文件,内容如下:

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

src/RevelApp/app/controllers/init.go中,我有以下代码(PS,我正在使用GorpController与MySQL交互):

func defineTodoItemTable(dbm *gorp.DbMap) {
  // 将"id"设置为主键并自增
  t := dbm.AddTable(models.TodoItem{}).SetKeys(true, "id")
  t.ColMap("name").SetMaxSize(25)
}

我遇到了一个错误:Go 代码src/RevelApp/app/controllers/init.go无法编译:undefined: models

我尝试过导入. "RevelApp/app/models",然后在models.TodoItem{}中省略models(如此处所述:https://stackoverflow.com/questions/24409631/revel-with-gorm-undefined-page),但我得到了错误:应用启动失败,revel/harness: 应用超时

这个链接是我找到的唯一与此问题相关的链接。我是否遗漏了什么?

编辑:$GOPATH:

/home/me/Source/go

models 的位置:

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

我如何导入 models 包:

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

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

package models

import (
  &quot;github.com/revel/revel&quot;
)

type TodoItem struct {
  Id              int64   `db:&quot;id&quot; json:&quot;id&quot;`
  Name            string  `db:&quot;name&quot; json:&quot;name&quot;`
}

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 &quot;id&quot; as primary key and autoincrement
  t := dbm.AddTable(models.TodoItem{}).SetKeys(true, &quot;id&quot;) 
  t.ColMap(&quot;name&quot;).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: https://stackoverflow.com/questions/24409631/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 (
    .&quot;RevelApp/app/models&quot;
    &quot;github.com/revel/revel&quot;
    &quot;github.com/coopernurse/gorp&quot;
    &quot;database/sql&quot;
    _ &quot;github.com/go-sql-driver/mysql&quot;
    &quot;fmt&quot;
    &quot;strings&quot;
)

答案1

得分: 1

你已经使用点号(.)导入限定符导入了你的模型。根据语言规范:

如果出现一个显式的句点(.)而不是名称,所有包的导出标识符将在当前文件的文件块中声明,并且可以在没有限定符的情况下访问。

这意味着你不需要使用models.TodoItem,而可以直接使用TodoItem

话虽如此,我建议避免这样做,并从导入语句中删除点号:

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

为什么呢?这样可以避免本地文件作用域变得拥挤,并且可以清楚地看到每个对象实际上位于哪里。

为了完整起见(你可能想知道它的作用,因为点号类似),在mysql包语句之前的下划线(_)基本上表示“导入这个包...但我不需要直接使用其中的任何内容”。这会触发包的init函数,使其能够在database/sql包的例程中注册自己。这就是为什么当你使用sql.DB类型时,最终会路由到MySql包的代码。

英文:

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 (
    &quot;RevelApp/app/models&quot;
    &quot;github.com/revel/revel&quot;
    &quot;github.com/coopernurse/gorp&quot;
    &quot;database/sql&quot;
    _ &quot;github.com/go-sql-driver/mysql&quot;
    &quot;fmt&quot;
    &quot;strings&quot;
)

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.

huangapple
  • 本文由 发表于 2014年11月12日 17:14:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/26883152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定