注意:
goroutinues机制可以方便实现异步处理
在启动新的goroutinues时,不应该使用原始上下文,必须使用它的只读副本
r.GET("/long_async", func(c *gin.Context) {
// create copy to be used inside the goroutine
//不能直接用c
cCp := c.Copy()
go func() {
// simulate a long task with time.Sleep(). 5 seconds
time.Sleep(5 * time.Second)
// note that you are using the copied context "cCp", IMPORTANT
log.Println("Done! in path " + cCp.Request.URL.Path)
}()
log.Println("主方法")
})