dshdsh2016 2016-09-17 18:42
浏览 208
已采纳

生成错误“ db.GetUsers未定义(类型* gorm.DB没有字段或方法GetUsers)”

I am new to golang and trying to make API server using gin + gorm.
I tried to build the code below but I am getting type *gorm.DB has no field or method GetUsers error.
This is a very simple API server and only I want to do is get all users from 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
}

I implemented GetUsers() in controllers/user.goand I also created users table.
I have no idea why it says no field or method GetUsers.Someone give me an advice to solve this.

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
}
  • 写回答

2条回答 默认 最新

  • duanshan5259 2016-09-19 00:53
    关注

    Tried to comment back to your question under Sergey's answer above, but I don't have the privilege since I'm new here.

    As Sergey said, I cannot see GetUsers function in gorm.DB struct. If you do db.Raw("select * from users").Scan(&users), you cannot (don't need to either) assign two variables to the statement. Instead, just:

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

    This is because DB.Scan() returns only one result variable in your DB implementation:

    // 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
    }
    

    Updated answer after more context was added to the question:

    That is because you didn't actually implement GetUsers for gorm.DB. What you did was - define a struct in controllers package named User, and attach a GetUsers method to that struct. Inside controllers/User.GetUsers you called repo.GetUsers which internally tries to call db.GetUsers. db is of type *gorm.DB which does not have GetUsers method defined.

    One way to fix is as Sergey suggested, just do db.Raw(...).Scan(...).

    If you really want to encapsulate gorm.DB and makes GetUsers look more native from the db connection, one thing you can try is:

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

    And in your models, you declare db as type *MyDB instead of *gorm.DB.

    For more official docs about this embedding technique, checkout https://golang.org/doc/effective_go.html#embedding

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵