异步协程
package main
import (
"gopkg.in/gin-gonic/gin.v1"
"time"
"log"
)
func main(){
// only set in Production
// gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.GET("/sync", func(c *gin.Context) {
time.Sleep(5 * time.Second)
//先Sleep 5S ,Print 后 返回http响应
log.Println("Done! in path" + c.Request.URL.Path)
})
router.GET("/async", func(c *gin.Context) {
//先返回http响应
// 异步 执行程序 Sleep 5S 后 Print
cCp := c.Copy()
go func() {
time.Sleep(5 * time.Second)
log.Println("Done! in path" + cCp.Request.URL.Path)
}()
})
router.Run(":8005")
}
