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"
)
You have imported your models using the dot (
.
) import qualifier. From the language spec:This means you don't need to use
models.TodoItem
.. you can simply useTodoItem
.That said .. I would suggest avoiding this and removing the dot from the import statement:
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 themysql
package statement basically says "import this package .. but I don't need to use anything in it directly". This fires the packagesinit
function allowing it to register itself with thedatabase/sql
packages routines. Which is why when you use thesql.DB
type it is eventually routed to the MySql package code.