。。。
package main import ( "log" "net/http" "time" "github.com/gin-gonic/gin" ) func main() { // 1.創建路由 // 默認使用了2個中間件Logger(), Recovery() r := gin.Default() // 1.異步 r.GET("/long_async", func(c *gin.Context) { // 需要搞一個副本 copyContext := c.Copy() c.JSON(http.StatusOK, "hehe") // 異步處理 go func() { time.Sleep(5 * time.Second) log.Println("異步執行:" + copyContext.Request.URL.Path) }() }) // 2.同步 r.GET("/long_sync", func(c *gin.Context) { time.Sleep(5 * time.Second) log.Println("同步執行:" + c.Request.URL.Path) c.JSON(http.StatusOK, "hehe") }) r.Run(":8000") }