Go实战--通过gin-gonic框架搭建restful api服务(github.com/gin-gonic/gin)
2017-08-08 14:56
591 查看
生命不止,继续 go go go !!!
先插播一条广告,给你坚持学习golang的理由:
《2017 软件开发薪酬调查:Go 和 Scala 是最赚钱的语言》
言归正传!
之前写过使用golang实现简单的restful api相关的博客:
Go实战–实现简单的restful api(The way to go)
其中,使用了github.com/gorilla/mux,今天要跟大家介绍的是gin-gonic/gin。
gin-gonic/gin
介绍:
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance – up to 40 times faster. If you need smashing performance, get yourself some Gin.
github地址:
https://github.com/gin-gonic/gin
获取:
例子:
运行:
通过浏览器访问:
http://localhost:8080/ping
全局设置环境:
或
获得路由实例:
构建restful api
创建五个routes:
下面我们要使用gin+mysql构建restful api。
如果对于golang中的MySQL不够熟悉的话,可以看看我之前写过的博文:
Go实战–go语言操作MySQL数据库(go-sql-driver/mysql)
先实现一个简单的根据id来FetchSingleUser吧:
我们事先通过命令行创建了user_info表以及一行数据,id=2, name=wangshubo
开始golang程序:
定义结构体:
check函数:
FetchSingleUser方法:
我们要返回json, gin对json也进行了封装,所以不再需要我们提供类似encoding/json之类的package。
浏览器访问:
http://localhost:8080/api/v1/userinfo/1
浏览器访问:
http://localhost:8080/api/v1/userinfo/2
gin输出log:
未完待续:
组织代码结构
先插播一条广告,给你坚持学习golang的理由:
《2017 软件开发薪酬调查:Go 和 Scala 是最赚钱的语言》
言归正传!
之前写过使用golang实现简单的restful api相关的博客:
Go实战–实现简单的restful api(The way to go)
其中,使用了github.com/gorilla/mux,今天要跟大家介绍的是gin-gonic/gin。
gin-gonic/gin
介绍:
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance – up to 40 times faster. If you need smashing performance, get yourself some Gin.
github地址:
https://github.com/gin-gonic/gin
获取:
go get github.com/gin-gonic/gin
例子:
package main import "github.com/gin-gonic/gin" func main() { r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong", }) }) r.Run() }
运行:
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET /ping --> main.main.func1 (3 handlers) [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default [GIN-debug] Listening and serving HTTP on :8080
通过浏览器访问:
http://localhost:8080/ping
// 20170808110257 // http://localhost:8080/ping { "message": "pong" }
全局设置环境:
gin.SetMode(gin.DebugMode)
或
gin.SetMode(gin.ReleaseMode)
获得路由实例:
r := gin.Default()
构建restful api
创建五个routes:
package main import ( "github.com/gin-gonic/gin" ) func main() { router := gin.Default() v1 := router.Group("/api/v1/userinfo") { v1.POST("/", CreateUser) v1.GET("/", FetchAllUsers) v1.GET("/:id", FetchSingleUser) v1.PUT("/:id", UpdateUser) v1.DELETE("/:id", DeleteUser) } router.Run() }
下面我们要使用gin+mysql构建restful api。
如果对于golang中的MySQL不够熟悉的话,可以看看我之前写过的博文:
Go实战–go语言操作MySQL数据库(go-sql-driver/mysql)
先实现一个简单的根据id来FetchSingleUser吧:
我们事先通过命令行创建了user_info表以及一行数据,id=2, name=wangshubo
开始golang程序:
定义结构体:
type Person struct { Id int Name string }
check函数:
func checkErr(err error) { if err != nil { panic(err) } }
FetchSingleUser方法:
我们要返回json, gin对json也进行了封装,所以不再需要我们提供类似encoding/json之类的package。
func FetchSingleUser(c *gin.Context) { id := c.Param("id") db, err := sql.Open("mysql", "root:wangshubo@/test?charset=utf8") checkErr(err) defer db.Close() err = db.Ping() checkErr(err) var ( person Person result gin.H ) row := db.QueryRow("select id, name from user_info where id = ?;", id) err = row.Scan(&person.Id, &person.Name) if err != nil { // If no results send null result = gin.H{ "result": nil, "count": 0, } } else { result = gin.H{ "result": person, "count": 1, } } c.JSON(http.StatusOK, result) }
浏览器访问:
http://localhost:8080/api/v1/userinfo/1
// 20170808141904 // http://localhost:8080/api/v1/userinfo/1 { "count": 0, "result": null }
浏览器访问:
http://localhost:8080/api/v1/userinfo/2
// 20170808141940 // http://localhost:8080/api/v1/userinfo/2 { "count": 1, "result": { "Id": 2, "Name": "wangshubo" } }
gin输出log:
[GIN] 2017/08/08 - 14:19:03 | 200 | 16.0127ms | ::1 | GET /api/v1/userinfo/1 [GIN] 2017/08/08 - 14:19:39 | 200 | 56.0381ms | ::1 | GET /api/v1/userinfo/2
未完待续:
组织代码结构
相关文章推荐
- Go实战--通过httprouter和redis框架搭建restful api服务(github.com/julienschmidt/httprouter)
- golang实战使用gin+xorm搭建go语言web框架restgo详解5.7 控制器数据响应
- go服务端----使用gin框架搭建简易服务
- golang实战使用gin+xorm搭建go语言web框架restgo详解5.9 控制器controller编程示例
- golang实战使用gin+xorm搭建go语言web框架restgo详解6.1 模型M和Orm
- golang实战使用gin+xorm搭建go语言web框架restgo详解1.1 go语言的困境
- golang实战使用gin+xorm搭建go语言web框架restgo详解6.4 推荐编程方式
- golang实战使用gin+xorm搭建go语言web框架restgo详解1.2 我要做什么
- golang实战使用gin+xorm搭建go语言web框架restgo详解7 视图层V
- golang实战使用gin+xorm搭建go语言web框架restgo详解2 框架基本架构
- golang实战使用gin+xorm搭建go语言web框架restgo详解8 关于模板
- golang实战使用gin+xorm搭建go语言web框架restgo详解10 使用restgo搭建后台管理系统
- golang实战使用gin+xorm搭建go语言web框架restgo详解3 系统常用配置参数
- golang实战使用gin+xorm搭建go语言web框架restgo详解9 session、日志、鉴权、验证码等
- golang实战使用gin+xorm搭建go语言web框架restgo详解4 路由配置
- java程序员10分钟可上手的golang框架golang实战使用gin+xorm搭建go语言web框架restgo
- golang实战使用gin+xorm搭建go语言web框架restgo详解5 控制器C
- golang实战使用gin+xorm搭建go语言web框架restgo详解5.2 跳转和重定向
- golang实战使用gin+xorm搭建go语言web框架restgo详解5.3 资源控制器
- golang实战使用gin+xorm搭建go语言web框架restgo详解5.4 控制器参数绑定