【问题标题】:go build error "db.GetUsers undefined (type *gorm.DB has no field or method GetUsers)"go build error "db.GetUsers undefined (type *gorm.DB has no field or method GetUsers)"
【发布时间】:2017-03-14 11:46:06
【问题描述】:

我是 golang 新手,正在尝试使用 gin + gorm 制作 API 服务器。
我尝试构建下面的代码,但收到 type *gorm.DB has no field or method GetUsers 错误。
这是一个非常简单的 API 服务器,我只想从 users 表中获取所有用户。

package models

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
)

var db *gorm.DB

func init() {
    var err error
    db, err = gorm.Open("postgres", "host=localhost dbname=test user=postgres password=test sslmode=disable")
    if err != nil {
        return err
    }
}

type User struct {
   ID           int    `json:"id"`
   Username     string `json:"name"`
}

func NewUser(id int, name string) User {
    return User{
      ID:           id,
      Username:     name,
    }
}

// UserRepository
type UserRepository struct {
}

func NewUserRepository() UserRepository {
    return UserRepository{}
}

func (m UserRepository) GetUsers() []*User {
    var users []*User
    has, _ := db.GetUsers(&users)
    if has {
         return users
    }
    return nil
}

我在controllers/user.go 中实现了GetUsers(),我还创建了users 表。
我不知道为什么它说no field or method GetUsers。有人给我一个解决这个问题的建议。

package controllers

import (
     "api-server/models"
)

type User struct {
}

func NewUser() User {
     return User{}
}

func (c User) GetUsers() interface{} {
     repo := models.NewUserRepository()
     user := repo.GetUsers()
     return user
}

【问题讨论】:

    标签: go go-gorm go-gin


    【解决方案1】:

    试图在上面 Sergey 的回答下回复您的问题,但我没有特权,因为我是新来的。

    正如 Sergey 所说,我在 gorm.DB 结构中看不到 GetUsers 函数。如果您执行db.Raw("select * from users").Scan(&users),则不能(也不需要)将两个变量分配给语句。相反,只需:

    db.Raw("select * from users").Scan(&users)
    

    这是因为 DB.Scan() 在您的数据库实现中只返回一个结果变量:

    // Scan scan value to a struct
    func (s *DB) Scan(dest interface{}) *DB {
        return s.clone().NewScope(s.Value).Set("gorm:query_destination",dest).callCallbacks(s.parent.callbacks.queries).db
    }
    

    在向问题添加更多上下文后更新答案:

    那是因为您实际上没有为gorm.DB 实现GetUsers。您所做的是 - 在 controllers 包中定义一个名为 User 的结构,并将 GetUsers 方法附加到该结构。在controllers/User.GetUsers 内部,您调用了repo.GetUsers,它在内部尝试调用db.GetUsersdb 属于 *gorm.DB 类型,它没有定义 GetUsers 方法。

    一种解决方法是按照 Sergey 的建议,只需执行 db.Raw(...).Scan(...)

    如果你真的想封装 gorm.DB 并使 GetUsers 从 db 连接看起来更原生,你可以尝试的一件事是:

    type MyDB struct {
        gorm.DB
    }
    func (m *MyDB) GetUsers() []*Users {
        // do things like m.Raw(...).Scan(...)
    }
    

    在您的模型中,您将 db 声明为类型 *MyDB 而不是 *gorm.DB

    有关此嵌入技术的更多官方文档,请查看https://golang.org/doc/effective_go.html#embedding

    【讨论】:

    • 感谢景超的评论。抱歉没有解释清楚。我在controllers/user.go中有GetUsers函数。我在控制器中导入了models包。
    • 好的,那是因为您在包User 中为结构体User 实现了GetUsers controller,但是在您的代码中您正在执行db.GetUsers(&users),其中db 的类型为*gorm.DB . *DB中的github.com/jinzhu/gorm本身并没有实现函数GetUsers
    【解决方案2】:

    如果我在您的代码中看到我没有看到 GetUsers 函数 db 这个 *gorm.DB 并且没有 GetUsers 你需要这样 db..Raw("select * from users").Scan(&users)

    【讨论】:

    • 感谢您的回复。我更改了代码,但has, _ := db.Raw("select * from users").Scan(&users) 返回assignment count mismatch: 2 = 1 错误。
    猜你喜欢
    • 2021-12-31
    • 2022-01-10
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多