mac highsierra use mysql
https://paiza.hatenablog.com/entry/2018/03/23/paizacloud_golang_revel)
I'm trying to use the Revel framework for mysql. It's revel Tutorial booking.
what is error?
ERROR 18:33:28 watcher.go:270: Build detected an error error="Go Compilation Error (in app/controllers/gorm.go:31): undefined: sql"
before
ERROR 18:18:26 watcher.go:270: Build detected an error error="Go Compilation Error (in app/controllers/app.go:26): c.Txn undefined (type Application has no field or method Txn)"
I did this because I got an error before
controllers/gorm.go
type Transactional struct {
*revel.Controller
Txn *sql.Tx
}
After running it results in error Please tell me how you can solve it
add controllers/app.go
package controllers
import (
"github.com/revel/revel"
"booking/app/models"
"booking/app/routes"
"database/sql"
)
type Application struct {
*revel.Controller
}
func (c Application) Index() revel.Result {
if c.connected() != nil {
return c.Redirect(routes.Hotels.Index())
}
c.Flash.Error("Please log in first")
return c.Render()
}
func (c Application) connected() *models.User {
if c.ViewArgs["user"] != nil {
return c.ViewArgs["user"].(*models.User)
}
if username, ok := c.Session["user"]; ok {
return c.getUser(username.(string))
}
return nil
}
controllers/gorm.go
package controllers
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/revel/revel"
"booking/app/models"
"log"
)
type GormController struct {
*revel.Controller
Txn *gorm.DB
}
func InitDB() {
dbInfo, _ := revel.Config.String("db.info")
db, err := gorm.Open("mysql", dbInfo)
if err != nil {
log.Panicf("Failed gorm.Open: %v
", err)
}
db.DB()
db.AutoMigrate(&models.Booking{})
db.AutoMigrate(&models.Hotel{})
db.AutoMigrate(&models.User{})
DB = db
}
func (c *GormController) SetDB() revel.Result {
c.Txn = DB
return nil
}
type Transactional struct {
*revel.Controller
Txn *sql.Tx
}
tree enter image description here maybe import miss?
edit app.go file
package controllers
import (
"github.com/revel/revel"
"booking/app/models"
"booking/app/routes"
"database/sql"
)
type Application struct {
*Gorm.Controller
}
edit gorm.go file
package controllers
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/revel/revel"
"booking/app/models"
"database/sql"
"log"
)
type GormController struct {
*revel.Controller
Txn *gorm.DB
}
But App have error.
revel run booking
Revel executing: run a Revel application
WARN 11:35:42 run.go:150: No http.addr specified in the app.conf listening on localhost interface only. This will not allow external access to your application
ERROR 11:35:42 reflect.go:176: Error: Failed to find import path for package=Gorm type=Controller
WARN 11:35:42 source_info.go:113: Type found in package: controllers, but did not embed from: revel.Controller foundstructures=GormController,Transactional,Static,TestRunner, name=Hotels importpath=booking/app/controllers
WARN 11:35:42 source_info.go:113: Type found in package: controllers, but did not embed from: revel.Controller name=Application importpath=booking/app/controllers foundstructures=GormController,Transactional,Static,TestRunner,
/Users/toshi/gocode/src/booking/app/controllers/app.go
ERROR 11:35:43 watcher.go:270: Build detected an error error="Go Compilation Error (in app/controllers/app.go:11): undefined: Gorm"
Please tell me how you can solve it