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 加一个K2暂停音乐的功能
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?