一、安装

GitHub gin框架
GitHub - 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.
image.png

下载并安装Gin

go get -u github.com/gin-gonic/gin

二、示例

  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. )
  5. func main() {
  6. // 创建一个默认的路由引擎
  7. r := gin.Default()
  8. // GET:请求方式;/hello:请求的路径
  9. // 当客户端以GET方法请求/hello路径时,会执行后面的匿名函数
  10. r.GET("/hello", func(c *gin.Context) {
  11. // c.JSON:返回JSON格式的数据
  12. c.JSON(200, gin.H{
  13. "message": "Hello world!",
  14. })
  15. })
  16. // 启动HTTP服务,默认在0.0.0.0:8080启动服务
  17. r.Run()
  18. }