乐闻世界logo
搜索文章和话题

What is the difference between github.com/jinzhu/gorm and gorm.io/gorm?

1个答案

1

github/jinzhu/orm and gorm.io/gorm both refer to the same project—GORM. It is a powerful Object-Relational Mapping (ORM) library developed in Go, used for handling database operations. However, the distinction between these two URLs primarily lies in the project's version and maintenance status.

1. github/jinzhu/orm

  • Author and Version: This is an early version of GORM, created and maintained by Jinzhu (a renowned Go developer).
  • Status: This version is no longer maintained. Jinzhu has ceased updates and support for this library.
  • GitHub Repository: The code for this version was hosted at github.com/jinzhu/gorm. Note that it is not github/jinzhu/orm; this is a common misconception. This version is known as GORM v1.

2. gorm.io/gorm

  • Author and Version: This is the current version of GORM, maintained by the Jinzhu team, but it has been migrated to the new website and organization gorm.io.
  • Status: This is an active version, continuously updated and maintained. It introduces numerous new features and improvements, including enhanced plugin support, context support, and improved relationship handling.
  • GitHub Repository: The code is hosted at github.com/go-gorm/gorm. This version is known as GORM v2.

Example and Changes

Using the creation and query of a user model as an example, demonstrate how both versions handle it:

GORM v1 (github.com/jinzhu/gorm)

go
package main import ( "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" ) type User struct { gorm.Model Name string } func main() { db, err := gorm.Open("sqlite3", "test.db") if err != nil { panic("failed to connect database") } defer db.Close() // Migrate the schema db.AutoMigrate(&User{}) // Create db.Create(&User{Name: "Jinzhu"}) // Read var user User db.First(&user, 1) // find user with id 1 }

GORM v2 (gorm.io/gorm)

go
package main import ( "gorm.io/gorm" "gorm.io/driver/sqlite" ) type User struct { ID uint Name string } func main() { db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) if err != nil { panic("failed to connect database") } // Migrate the schema db.AutoMigrate(&User{}) // Create db.Create(&User{Name: "GORM"}) // Read var user User db.First(&user, 1) // find user with id 1 }

Summary

The primary difference between these URLs is the version and maintenance status. For new projects, it is recommended to use gorm.io/gorm as it provides the latest features and robust support. Existing projects using v1 can continue with it if no upgrade is needed, but be aware that future security and feature requirements may necessitate an upgrade.

2024年8月12日 17:04 回复

你的答案