1

definition

GORM (Golang Object Relational Mapping): Map an instance of an object or structure in Go to a piece of data in the database through ORM statements (instead of SQL).

ORM has the following correspondence:

  • datatable<==>struct
  • data row <==> struct instance
  • field <==> struct field

Install

First of all, if you are in China, running the installation command directly may have the same timeout problem as installing gin. If this problem occurs, you can first enter it on the command line:

 export GO111MODULE=on
export GOPROXY=https://goproxy.cn

Then run the install command again
go get -u gorm.io/gorm


Connect to the database

 package main

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
)

func main() {
    db, _ := gorm.Open("mysql", "user:password@(localhost)/dbname?charset=utf8mb4&parseTime=True&loc=Local")
    defer db.Close()
}
refer to:
bilibili
https://gorm.io/zh_CN/docs/

LiberHome
409 声望1.1k 粉丝

有问题 欢迎发邮件 📩 liberhome@163.com