go revel undefined:sql或Txn

问题描述 投票:0回答:1

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

我这样做是因为我之前收到了错误

控制器/ gorm.go

type Transactional struct {
    *revel.Controller
    Txn *sql.Tx
}

运行后导致错误请告诉我如何解决它

添加控制器/ 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
}

控制器/ 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可能导入错过?

go revel
1个回答
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
}

但是App有错误。

    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" 

请告诉我你如何解决它

© www.soinside.com 2019 - 2024. All rights reserved.