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方法)

  • 相关阅读:
    bzoj 1057: [ZJOI2007]棋盘制作
    【NOIP2012】开车旅行
    bzoj 2326: [HNOI2011]数学作业
    一本通1527欧拉回路
    一本通1530 Ant Trip
    一本通1528单词游戏
    luogu1856
    CF1045G
    10.18模拟赛
    10.16模拟赛
  • 原文地址:https://www.cnblogs.com/ybf-yyj/p/11889923.html
Copyright © 2011-2022 走看看