【Gin】框架学习

121 阅读1分钟
  • Go语言学习相关网址&文档

  • Go语言镜像及Gin框架下载

    GOPROXY=https://goproxy.cn,direct
    

    1688370536099.png

  • Gin框架的下载与安装

    • 安装Gin框架
    go get -u github.com/gin-gonic/gin
    
    • 搭建Gin框架环境验证
    方式一:
    package main
    
    import "github.com/gin-gonic/gin"
    
    func main() {
       //  创建一个默认的路由
       router := gin.Default()
    
       //  绑定路由规则及函数,访问 /index 的路由
       router.GET("/index", func(context *gin.Context) {
          context.String(200, "Hello 刘哈哈")
       })
    
       router.Run(":9999")
    }
    
    方式二:
    package main
    import "github.com/gin-gonic/gin"
    
    func Index(context *gin.Context) {
       context.String(200, "Hello 刘哈哈")
    }
    func main() {
       //  创建一个默认的路由
       router := gin.Default()
       //  绑定路由规则
       router.GET("/index", Index)
       // 启动监听gin会把web服务运行在本机0.0.0.0:9999cmd
       // 访问:ipconfig  curl http://IpV4的地址:9999/index
       router.Run(":9999")
           
        //  采用原生的http方式启动服务
        http.ListenAndServe(":9999", router)
    }