Gin是一个用Go语言编写的web框架。它是一个类似于martini但拥有更好性能的API框架, 由于使用了httprouter,速度提高了近40倍。

安装

要安装Gin软件包,需要安装Go并首先设置Go工作区。

1.首先需要安装Go(需要1.10+版本),然后可以使用下面的Go命令安装Gin。

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

2.导入代码:

import “github.com/gin-gonic/gin”

gin示例

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. // 1.创建路由
  8. r := gin.Default()
  9. // 2.绑定路由规则,执行的函数
  10. // gin.Context,封装了request和response
  11. r.GET("/", func(c *gin.Context) {
  12. c.String(http.StatusOK, "hello World!")
  13. })
  14. // 3.监听端口,默认在8080
  15. // Run("里面不指定端口号默认为8080")
  16. r.Run(":8000")
  17. }