zoukankan      html  css  js  c++  java
  • gin框架初识(先跑一个简单demo) ①

    Gin 是一个 go 写的 web 框架,具有高性能的优点。官方地址:https://github.com/gin-gonic/gin

    先跑一个demo(先安装gin框架,具体见官方地址)

    1.vscode新建文件夹project,文件夹中新建一个go文件,index.go

    index.go文件如下:

    package main
    import (
        "fmt"
        "github.com/gin-gonic/gin"
    )
    func main() {
        r := gin.New()
        r.Use(gin.Logger())
    
        r.Use(gin.Recovery())
    
        r.GET("/first", func(c *gin.Context) {
            fmt.Println("first .........")
        })
    
        authorized := r.Group("/try")
    
        authorized.POST("/second", second)
        authorized.POST("/third", third)
    
        // 嵌套路由组
        testing := authorized.Group("testing")
        testing.GET("/forth", fourth)
    
        // 监听并在 0.0.0.0:8080 上启动服务
        r.Run(":8080")
    }
    
    func second(c *gin.Context) {
        fmt.Println("second .........")
    }
    
    func third(c *gin.Context) {
        fmt.Println("third .........")
    }
    
    func fourth(c *gin.Context) {
        fmt.Println("fourth .........")
    }

    2.go中代码必须在gopath的目录下运行,为了摆脱这个限制,执行 go mod init project,这时会生成go.mod go.sum两个文件

    3.运行一下这个代码:go run index.go

     上图可以看到,你的所有路由路径

    4,.利用postman来测试这些链接是否能够正常运行(注意选择post和get方法)

  • 相关阅读:
    AJAX 大全
    has value '1.8', but '1.7' is required
    VS2010官方下载地址
    win10桌面显示我的电脑
    使用 CAST
    for循环+canvas实现黑客帝国矩形阵
    C# Lambda
    win7系统部分便笺的元数据已被损坏怎么恢复
    SQL查询所有表,所有列
    truncate和delete之间有什么区别
  • 原文地址:https://www.cnblogs.com/ybf-yyj/p/11889923.html
Copyright © 2011-2022 走看看