20240208学习Golang gin框架

gin框架初体验

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

import (
  "net/http"

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

func main() {
  r := gin.Default()
  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })
  r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

gin路由分组

func main() {
    r := gin.Default()

    //路由分组
    user := r.Group("/user")
    {
        user.GET("/hello", func(c *gin.Context) {
            c.String(http.StatusOK, "world")
        })
    }

    r.Run(":8888") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}