mac highsierra 使用mysql
https://paiza.hatenablog.com/entry/2018/03/23/paizacloud_golang_revel)
我正在尝试将Revel框架用于mysql。 这是启示性的教程预订。
什么是错误?
ERROR 18:33:28 watcher.go:270: Build detected an error error="Go Compilation Error (in app/controllers/gorm.go:31): undefined: sql"
之前
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)"
之所以这样做,是因为之前有一个错误
controllers / gorm.go
type Transactional struct {
*revel.Controller
Txn *sql.Tx
}
运行后会导致错误 请告诉我你怎么解决
添加 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\n", 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
}
树 enter image description here 也许进口小姐?
答案 0 :(得分:0)
编辑app.go文件
package controllers
import (
"github.com/revel/revel"
"booking/app/models"
"booking/app/routes"
"database/sql"
)
type Application struct {
*Gorm.Controller
}
编辑gorm.go文件
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
}
但是应用程序有错误。
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"
请告诉我如何解决