Roh*_*kla 5 postgresql go go-gorm glide-golang
由于我没有在 Close() 函数中找到 *gorm 实例,任何帮助将不胜感激
dbURI := fmt.Sprintf("user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s",
    "username", "password", "dbname", "5432", "disable", "Asia/Kolkata")
fmt.Println(dbURI)
connection, err := gorm.Open(postgres.Open(dbURI), &gorm.Config{})
if err != nil {
    fmt.Println("Error connecting database")
    panic(err.Error())
} else {
    fmt.Println("Connected to database")
}
Run Code Online (Sandbox Code Playgroud)
注意:connection.Close() 不适用于 GORM 1.20.0
小智 10
Jinzhu decided to eliminate the Close() method on version 1.20 because GORM supports connection pooling, so the correct use would be to open a connection and share it within your application.
如果您的特定用例仍然需要使用该Close()方法,GORM 提供了方法 DB,该方法会返回一个db generic_interface,您可以在其中使用它。
例如
sqlDB, err := db.DB()
// Close
sqlDB.Close()
Run Code Online (Sandbox Code Playgroud)
        Tho*_*ach 10
我认为您可以使用以下代码来关闭数据库连接:
sqlDB, err := connection.DB()
if err != nil {
    log.Fatalln(err)
}
defer sqlDB.Close()
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           4985 次  |  
        
|   最近记录:  |