安装Gin软件包

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

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

  1. 将其导入代码

import "github.com/gin-gonic/gin"

hello world

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