注意:
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("主方法")
})