https://gorm.io/docs/

安装:

<span class="line">go get -u gorm.io/gorm</span>
<span class="line">go get -u gorm.io/driver/sqlite</span>

Quick Start

使用postgres:

go get gorm.io/driver/postgres

连接数据库,创建新表,创建数据crud:

package main
import (<br />
&nbsp; &quot;gorm.io/gorm&quot;<br />
&nbsp; //&nbsp; &quot;gorm.io/driver/sqlite&quot;<br />
&nbsp; //引入sql<br />
&nbsp; &quot;gorm.io/driver/postgres&quot;<br />
&nbsp; //引入 postgres<br />
)
type Product struct {<br />
&nbsp; gorm.Model<br />
&nbsp; Code&nbsp; string<br />
&nbsp; Price uint<br />
}
type Apple struct {<br />
&nbsp;&nbsp; &nbsp;Id&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp; `gorm:&quot;primary_key&quot;`<br />
&nbsp;&nbsp; &nbsp;Name string `gorm:&quot;type:varchar(50);not null;index:ip_idx&quot;`<br />
&nbsp;&nbsp; &nbsp;Color int&nbsp;&nbsp;&nbsp; `gorm:&quot;not null&quot;`<br />
&nbsp;&nbsp; &nbsp;Addr string `gorm:&quot;type:varchar(50);not null;&quot;`<br />
}
func main() {<br />
&nbsp; dsn := &quot;host=localhost user=admin password=88888888 dbname=blogs port=5432 sslmode=disable TimeZone=Asia/Shanghai&quot;<br />
&nbsp; db, err := gorm.Open(postgres.Open(dsn), &amp;gorm.Config{})<br />
&nbsp; if err != nil {<br />
&nbsp;&nbsp;&nbsp; panic(&quot;failed to connect database&quot;)<br />
&nbsp; }
&nbsp; // Migrate the schema<br />
&nbsp; db.AutoMigrate(&amp;Product{})
&nbsp; // Create<br />
&nbsp; db.Create(&amp;Product{Code: &quot;D42&quot;, Price: 100})
&nbsp; // Read<br />
&nbsp; var product Product<br />
&nbsp; db.First(&amp;product, 1) // find product with integer primary key<br />
&nbsp; db.First(&amp;product, &quot;code = ?&quot;, &quot;D42&quot;) // find product with code D42
&nbsp; // Update - update product&#39;s price to 200<br />
&nbsp; db.Model(&amp;product).Update(&quot;Price&quot;, 200)<br />
&nbsp; // Update - update multiple fields<br />
&nbsp; db.Model(&amp;product).Updates(Product{Price: 200, Code: &quot;F42&quot;}) // non-zero fields<br />
&nbsp; db.Model(&amp;product).Updates(map[string]interface{}{&quot;Price&quot;: 200, &quot;Code&quot;: &quot;F42&quot;})
&nbsp; // Delete - delete product<br />
&nbsp; db.Delete(&amp;product, 1)<br />
}

可以看到数据库已经创建了新表,并更新了数据。