gorm 数据库表模型声明 - 基础 链接数据库 1 2 3 4 5 6 7 8 9 10 import ( "gorm.io/driver/mysql" "gorm.io/gorm" ) func main() { // refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local" db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) } 常用字段类型与 gorm 默认字段类型 varchar, int, datetime, timestamp 表定义如下 1 2 3 4 5 type Author struct { gorm.Model Name string Password string } auto migrate 后, 可以看到 name, password 默认使用的是 longtext 类型。 1 2 3 4 5 6 7 8 9 10 11 12 show create table authors; CREATE TABLE `authors` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `created_at` datetime(3) DEFAULT NULL, `updated_at` datetime(3) DEFAULT……

阅读全文