go - golang gorm包装功能

标签 go go-gorm

我喜欢做的事情是,将gorm功能包装在没有特定结构参数的某些功能后面。

func (e *Engine) Create(object interface{}) error {
    tx := e.db.Begin()
    if err := tx.Create(&object).Error; err != nil {
        tx.Rollback()
        return err
    }
    tx.Commit()

    return nil
}

存在使用db.CreateTable(&<specific struct{})创建的特定表。
但是它失败并显示以下错误:
Can't create <specific struct>: no such table: 

这是与gorm / golang结合使用的一种方法,以及如何包装?

最佳答案

请参阅Adrian的评论。非常感谢,睁开我疲倦的眼睛。

传递object而不是&object

关于go - golang gorm包装功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48191229/

相关文章:

go - 使用 map[string]int 作为 map[interface{}]interface{} 类型的参数

go - 使用db.Create和db.FirstOrCreate将数据保存到数据库时, "ID"列的值为 "0"

database - 这种数据访问模式是线程安全的吗?

c - 在 C90 中实现无溢出系统堆栈

go - 在 golang 中,如果文件不存在,则 os.OpenFile 不会返回 os.ErrNotExist

go - 如何在 go 中使用字符串文字

testing - 使用 go test 跳过一些测试

go - 如何排除列被选中

mysql - 乱码 : json of json not work

gorm 使用 find 返回空