# Gin
# 介绍
Gin 是一个用 Go 语言 (Golang) 编写的 HTTP web 框架。它具有类似 Martini 的 API,但性能比 Martini 快 40 倍。如果你需要极好的性能,使用 Gin 吧。
Gin 的官方文档:文档
# 获取
1 go get -u github.com/gin-gonic/gin
# 使用样例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package mainimport ( "fmt" "github.com/gin-gonic/gin" "net/http" ) func main () { r := gin.Default() r.GET("/" , func (c *gin.Context) { c.JSON(http.StatusOK, gin.H{"msg" :"Hello World!" }) }) _ = r.Run() }
使用 go run 或者 go build 运行,打开地址 localhost:8080 即可
# 常用函数介绍 (快速入门)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 func Default () (*Engine)func (group *RouterGroup) Group(path string , handlers ...HandlerFunc) *RouterGroupfunc (group *RouterGroup) Static(relativePath string , root string ) IRoutesfunc (engine *Engine) LoadHTMLGlob(pattern string )func (engine *Engine) LoadHTMLFiles(files ...string )func (group *RouterGroup) GET(path string , handlers ...HandlerFunc) IRoutesfunc (group *RouterGroup) POST(path string , handlers ...HandlerFunc) IRoutesfunc (group *RouterGroup) PUT(path string , handlers ...HandlerFunc) IRoutesfunc (group *RouterGroup) DELETE(path string , handlers ...HandlerFunc) IRoutesfunc (engine *Engine) NoRoute(handlers ...HandlerFunc)func (c *Context) HTML(code int , name string , obj any)func (c *Context) JSON(code int , obj any)func (c *Context) BindJSON(obj any) error func (c *Context) Param(key string ) string func (ps Params) Get(name string ) (string , bool )func (c *Context) Query(key string ) (value string )func (c *Context) PostForm(key string ) (value string )func (engine *Engine) Run(addr ...string ) (err error )
# 响应样例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package mainimport ( "fmt" "github.com/gin-gonic/gin" "net/http" ) func main () { r := gin.Default() r.POST("/" , func (c *gin.Context) { c.JSON(http.StatusOK, gin.H{"msg" :"增" }) }) r.GET("/" , func (c *gin.Context) { c.JSON(http.StatusOK, gin.H{"msg" :"查" }) }) r.PUT("/" , func (c *gin.Context) { c.JSON(http.StatusOK, gin.H{"msg" :"改" }) }) r.DELETE("/" , func (c *gin.Context) { c.JSON(http.StatusOK, gin.H{"msg" :"删" }) }) _ = r.Run() }
# Gorm
# 介绍
Golang 出色的 ORM 库,旨在对开发人员友好
gorm 是国人开发的一个 ORM 库,想学的话读文档更好一些,官方文档 。
# 获取
1 go get -u github.com/jinzhu/gorm
# 使用样例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package mainimport ( "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" ) type User struct { gorm.Model Name string `gorm:"default:'匿名用户';type:varchar(20)"` Age int32 } func main () { usrAndPwd := "username:password" host := "@(127.0.0.1:3306)" otherArgs := "/database01?charset=utf8mb4&parseTime=True&loc=Local" db, err := gorm.Open("mysql" , usrAndPwd + host + otherArgs) if err != nil { fmt.Println("database connect failed, err:" , err) return } defer db.Close() db.AutoMigrate(&User{}) db.Create(&User{ Age: 18 , }) }
# 常用函数介绍 (快速入门)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 func Open (dialect string , args ...interface {}) (db *DB, err error )func (s *DB) Close() error func (s *DB) Debug() *DBfunc (s *DB) AutoMigrate(values ...interface {}) *DBfunc (s *DB) Create(value interface {}) *DBfunc (s *DB) First(out interface {}, where ...interface {}) *DBfunc (s *DB) Take(out interface {}, where ...interface {}) *DBfunc (s *DB) Last(out interface {}, where ...interface {}) *DBfunc (s *DB) Save(value interface {}) *DBfunc (s *DB) Update(attrs ...interface {}) *DBfunc (s *DB) Updates(values interface {}, ignoreProtectedAttrs ...bool ) *DBfunc (s *DB) Delete(value interface {}, where ...interface {}) *DBfunc (s *DB) Select(query interface {}, args ...interface {}) *DBfunc (s *DB) Model(value interface {}) *DBfunc (s *DB) Where(query interface {}, args ...interface {}) *DB
# 立即执行方法
立即执行方法是指那些会立即生成 SQL 语句并发送到数据库的方法,他们一般是 CRUD 方法,比如:
Create,First,Find,Take,Save,Update,Delete,Scan,Row,Rows…
# 对应关系
Gorm 中存在如下的对应关系:
Model
table
object
row
filed
filed
模型对应整表,实例对应记录,字段对应字段