duanquan1876 2019-05-17 14:26
浏览 548
已采纳

Golang和GORM。 如何正确地将模型类型传递给方法?

I have next GORM model

package entity

import (
    "github.com/jinzhu/gorm"
)

type InterfaceEntity interface {
}

type User struct {
    InterfaceEntity
    gorm.Model
    Name string
}

I try to pass GORM entity type into base crud repository. My base crud repository:

package repository

import (
    "billingo/model/entity"
    "fmt"
    "github.com/jinzhu/gorm"
    "reflect"
)

type CrudRepository struct {
    *BaseRepository
}

func NewCrudRepository(db *gorm.DB) CrudRepositoryInterface {
    repo := NewBaseRepository(db).(*BaseRepository)
    return &CrudRepository{repo}
}

func (c CrudRepository) Find(id uint, item entity.InterfaceEntity) entity.InterfaceEntity {
    fmt.Println("--- Initial")
    var local entity.User
    fmt.Println("--- local: ", reflect.TypeOf(local), local)
    fmt.Println("--- Item:  ", reflect.TypeOf(item), item)


    fmt.Println("--- Values")
    c.db.First(&local, id)
    fmt.Println("--- local: ", reflect.TypeOf(local), local)
    c.db.First(&item, id)
    fmt.Println("--- Item: ", reflect.TypeOf(item), item)
    return item
}

As you can see here are item and local variables in Find() method.

I pass item using next way from service:

func (c CrudService) GetItem(id uint) entity.InterfaceEntity {
    var item entity.User
    return c.repository.Find(id, item)
}

It seems what local and item must be equals and behavior must be equivalent.

But output is

--- Initial
--- local:  entity.User {<nil> {0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }
--- Item:   entity.User {<nil> {0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }
--- Values
--- local:  entity.User {<nil> {1 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} test 1}
--- Item:  entity.User {<nil> {0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }
INFO[0000] User info                                     user="{<nil> {0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }" user-id=1

(/home/mnv/go/src/billingo/model/repository/CrudRepository.go:29) 
[2019-05-17 17:07:37]  unsupported destination, should be slice or struct 

item passed from service leads to message

unsupported destination, should be slice or struct

How to pass item correctly, I need behavior like with local?

  • 写回答

2条回答 默认 最新

  • dsfdsf46465 2019-05-18 13:33
    关注

    Oh, I has fixed it.

    Now repository method Find called from service with &item:

    func (c CrudService) GetItem(id uint) entity.InterfaceEntity {
        var item entity.User
        return c.repository.Find(id, &item)
    }
    

    And repository method pass item without &:

    func (c CrudRepository) Find(id uint, item entity.InterfaceEntity) entity.InterfaceEntity {
        c.db.First(item, id)
        return item
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能