.\store.go:32:10: cannot assign *gorm.DB to Db (type gorm.DB) in multiple assignment

原文地址:https://blog.csdn.net/weixin_39777626/article/details/87095893

原码

var Db gorm.DB

func init() {
	var err error
	Db, err = gorm.Open("postgres", "user=postgres dbname=testdb password=3141596 sslmode=disable")
	if err != nil {
		panic(err)
	}
	Db.AutoMigrate(&Post{}, &Comment{})
}

报错

.\store.go:32:10: cannot assign *gorm.DB to Db (type gorm.DB) in multiple assignment
  •  

纠正
如上提示,Db 代表 gorm.DB

无法将 *gorm.DB 转为 gorm.DB ,因为他们类型不同
所以我们要让他们变为同一类,只需在 gorm.DB 前加指针 *

var Db *gorm.DB

func init() {
	var err error
	Db, err = gorm.Open("postgres", "user=postgres dbname=testdb password=3141596 sslmode=disable")
	if err != nil {
		panic(err)
	}
	Db.AutoMigrate(&Post{}, &Comment{})
}

猜你喜欢

转载自blog.csdn.net/dodod2012/article/details/87864038
DB
今日推荐