我有下一个 GORM 模型
package entity
import (
"github.com/jinzhu/gorm"
)
type InterfaceEntity interface {
}
type User struct {
InterfaceEntity
gorm.Model
Name string
}
我尝试将 GORM 实体类型传递到基础 crud 存储库中。我的基础 crud 存储库:
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
}
正如你在这里看到的,方法中item
的local
变量Find()
。
我通过item
服务使用下一个方式:
func (c CrudService) GetItem(id uint) entity.InterfaceEntity {
var item entity.User
return c.repository.Find(id, item)
}
似乎什么local
和item
必须相等,行为必须相等。
但输出是
--- 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
从服务传递到消息
不受支持的目的地,应该是切片或结构
如何item
正确通过,我需要类似 with 的行为local
?