我是 golang 和 gorm 框架的新手。這是我使用 gorm 獲取數(shù)據(jù)庫(kù)的代碼。數(shù)據(jù)庫(kù)Gopackage dbimport ( "fmt" "github.com/jinzhu/gorm" _ "github.com/lib/pq" "github.com/teamhide/hfive_go/users" "log")const ( DbHost = "localhost" DbPort = "5432" DbUser = "1" DbPassword = "1" DbName = "1")var db *gorm.DBvar err errorfunc Init() { db, err = gorm.Open( "postgres", fmt.Sprintf( "host=%s port=%s user=%s dbname=%s password=%s sslmode=disable", DbHost, DbPort, DbUser, DbName, DbPassword, ), ) if err != nil { log.Fatal(err.Error()) } db.AutoMigrate(&users.User{}) defer db.Close()}func GetDB() *gorm.DB { return db}用戶(hù)/usecase.gopackage usersimport ( "fmt" "github.com/teamhide/gin_boilerplate/db")type UserUsecase interface { RegisterUserUsecase(email, password1, password2 string) (bool, error)}func RegisterUserUsecase(email, password1, password2 string) (bool, error) { db := db.GetDB() fmt.Println(db) return true, nil}在用例中,如您所知,我只是打印了 db 變量來(lái)檢查它是否具有正確的數(shù)據(jù)庫(kù)數(shù)據(jù)。但是當(dāng)我執(zhí)行它時(shí),它會(huì)拋出錯(cuò)誤。...runtime error: invalid memory address or nil pointer dereference.../Users/hide/go/src/github.com/teamhide/hfive_go/users/controllers.go:18 (0x15b3301) UserController.RegisterDefaultUser: uc.RegisterUserUsecase(form.Email, form.Password1, form.Password2)...我的代碼有問(wèn)題嗎?
gorm 無(wú)效內(nèi)存地址或 nil 指針取消引用
料青山看我應(yīng)如是
2023-08-14 16:53:14