去构建错误 "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)"
我是 golang 的新手,正在尝试使用 gin + gorm 创建 API 服务器。
我尝试构建下面的代码,但出现 type *gorm.DB has no field or method GetUsers
错误。
这是一个非常简单的 API 服务器,我只想从 users
table.
获取所有用户
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
table。
我不知道为什么它说 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
}
如果我在你的代码中看到我没有看到 GetUsers 函数
db 这个 *gorm.DB 并且没有 GetUsers
你需要这样
db..Raw("select * from users").Scan(&users)
试图在上面谢尔盖的回答下回复你的问题,但我没有特权,因为我是新来的。
正如谢尔盖所说,我在 gorm.DB
结构中看不到 GetUsers 函数。如果你这样做 db.Raw("select * from users").Scan(&users)
,你不能(也不需要)将两个变量分配给语句。相反,只是:
db.Raw("select * from users").Scan(&users)
这是因为 DB.Scan() returns 在您的数据库实现中只有一个结果变量:
// 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
。您所做的是 - 在名为 User
的 controllers
包中定义一个结构,并将 GetUsers
方法附加到该结构。在 controllers/User.GetUsers
中,您调用了 repo.GetUsers
,它在内部尝试调用 db.GetUsers
。 db
的类型 *gorm.DB
没有定义 GetUsers
方法。
按照 Sergey 的建议,一种修复方法是 db.Raw(...).Scan(...)
。
如果你真的想封装 gorm.DB 并使 GetUsers
从数据库连接看起来更原生,你可以尝试的一件事是:
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
我是 golang 的新手,正在尝试使用 gin + gorm 创建 API 服务器。
我尝试构建下面的代码,但出现 type *gorm.DB has no field or method GetUsers
错误。
这是一个非常简单的 API 服务器,我只想从 users
table.
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
table。
我不知道为什么它说 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
}
如果我在你的代码中看到我没有看到 GetUsers 函数
db 这个 *gorm.DB 并且没有 GetUsers
你需要这样
db..Raw("select * from users").Scan(&users)
试图在上面谢尔盖的回答下回复你的问题,但我没有特权,因为我是新来的。
正如谢尔盖所说,我在 gorm.DB
结构中看不到 GetUsers 函数。如果你这样做 db.Raw("select * from users").Scan(&users)
,你不能(也不需要)将两个变量分配给语句。相反,只是:
db.Raw("select * from users").Scan(&users)
这是因为 DB.Scan() returns 在您的数据库实现中只有一个结果变量:
// 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
。您所做的是 - 在名为 User
的 controllers
包中定义一个结构,并将 GetUsers
方法附加到该结构。在 controllers/User.GetUsers
中,您调用了 repo.GetUsers
,它在内部尝试调用 db.GetUsers
。 db
的类型 *gorm.DB
没有定义 GetUsers
方法。
按照 Sergey 的建议,一种修复方法是 db.Raw(...).Scan(...)
。
如果你真的想封装 gorm.DB 并使 GetUsers
从数据库连接看起来更原生,你可以尝试的一件事是:
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