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

1.8k views Asked by At

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

There are 1 answers

2
Simon Whitehead On BEST ANSWER

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.