gpt4 book ai didi

database - 无法在 Go 中定义来自另一个包的接收器

转载 作者:IT王子 更新时间:2023-10-29 02:27:36 25 4
gpt4 key购买 nike

我是 Golang 的初学者,无法理解这门语言中的一些概念。很喜欢,但是网上的例子都很简单,没有说明正确的开发方式。
我想配置与 MySQL 的数据库连接。我创建了一个包含文件 dbconfig.go 的包 dbconfig 和包含接口(interface)文件的包 dastructure 以及另一个包含实体文件的包 entity
这是结构:[ 1]

ma​​in.go:

import (
y "github.com/danyalov/shebeke/dbconfig"
"github.com/danyalov/shebeke/routes"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/gommon/log"
)

func main() {
db, err := y.InitDB("mysql", "root:root@tcp(localhost:3306)/dbtest?parseTime=true")
if err != nil {
log.Fatal(err)
}
e := routes.NewConnection(db)
e.Logger.Fatal(e.Start(":9898"))
}

routes.go:

import (
"github.com/danyalov/shebeke/datastructure"
y "github.com/danyalov/shebeke/dbconfig"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)

func NewConnection(db *y.DB) *echo.Echo {
e := echo.New()
env := Env{db}
e.Use(middleware.Logger())
e.Use(middleware.Recover())

e.GET("/contracts", env.GetContracts)
e.GET("/contract/:id", env.GetContractByID)

return e
}

type Env struct {
contract datastructure.Contract
}

services.go:

import (
"github.com/labstack/echo"
"log"
"net/http"
"strconv"
)

func (env *Env) GetContracts(c echo.Context) error {
contracts, err := env.contract.GetContracts()
if err != nil {
log.Fatal(err)
}
return c.JSON(http.StatusOK, &contracts)
}

dbconfig.go:

import (
"database/sql"
"fmt"
"github.com/labstack/gommon/log"
)

type DB struct {
*sql.DB
}

//InitDB initialize mysql database
func InitDB(driver, path string) (*DB, error) {
db, err := sql.Open(driver, path)
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
} else {
fmt.Println("Connected to DB")
}
return &DB{db}, err
}

datastructure/contract.go:

import y "github.com/danyalov/shebeke/datastructure/entity"

type Contract interface {
GetContracts() (y.Contracts, error)
GetContractByID(id int) (y.Contract, error)
}

datastructure/entity/contract.go:

import (
"github.com/labstack/gommon/log"
"time"
)

type Contract struct {
ID int `json:"id"`
State string `json:"state"`
StartDate time.Time `json:"start_date"`
FinishDate time.Time `json:"finish_date"`
}

type Contracts []Contract

func (db *DB) GetContracts() (c Contracts, err error) {
rows, err := db.Query("select * from contract")
if err != nil {
log.Fatal(err)
}

contract := Contract{}
for rows.Next() {
err = rows.Scan(&contract.ID, &contract.State, &contract.StartDate, &contract.FinishDate)
c = append(c, contract)
}
return c, err
}

为什么我不能将 dbconfig 包中的 DB 类型作为方法接收器导入到 entity 包中?我收到 Unresolved type 'DB' 错误。

这是 my working copy(Git)在这个项目中,我将 dbconfig.go 放在实体中,但我不喜欢它,我认为它是 dbconfig 文件的不正确位置。

在 Go 中配置 db 的正确文件结构是什么?也许您有自己的 Git 示例或一些教程?

最佳答案

您只能在同一包 中定义的类型上定义方法。在这种情况下,您的 DB 类型是在您的 dbconfig 包中定义的,因此您的 entity 包不能在其上定义方法。

在这种情况下,您的选择是让 GetContracts 成为函数而不是方法,并将 *dbconfig.DB 作为参数传递给它,或者通过导入您的 实体来反转依赖关系dbconfig 中打包并在那里编写 GetContracts(作为方法或函数,两种方式都有效)。第二个实际上可能是更好的选择,因为从设计的角度来看,它打破了抽象,让除了数据库包之外的包创建 SQL 查询字符串。

关于database - 无法在 Go 中定义来自另一个包的接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41109708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com