NOTE : Pour gérer time.Time correctement, vous devez inclure parseTime comme paramètre. (plus de paramètres) Pour supporter pleinement l’encodage UTF-8, vous devez changer charset=utf8 à charset=utf8mb4. Voir cet article pour une explication détaillée
db, err := gorm.Open(mysql.New(mysql.Config{ DSN: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8&parseTime=True&loc=Local", // data source name DefaultStringSize: 256, // default size for string fields DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6 DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB DontSupportRenameColumn: true, // `change` when rename column, rename column not supported before MySQL 8, MariaDB SkipInitializeWithVersion: false, // auto configure based on currently MySQL version }), &gorm.Config{})
Personnaliser le Driver
GORM permet de personnaliser le Driver MySQL avec l’option DriverName par exemple :
import ( "gorm.io/driver/sqlite"// Sqlite driver based on CGO // "github.com/glebarez/sqlite" // Pure go SQLite driver, checkout https://github.com/glebarez/sqlite for details "gorm.io/gorm" )
NOTE: You can also use file::memory:?cache=shared instead of a path to a file. This will tell SQLite to use a temporary database in system memory. (See SQLite docs for this)