How to change external variable's value inside a goroutine closure
问题 func (this *l) PostUpload(ctx *Context) { //ctx.Response.Status = 500 l, err := models.NewL(this.Config) go func() { err = l.Save(file) if err != nil { ctx.Response.Status = 500 ctx.Response.Body = err } else { ctx.Response.Status = 204 } }() } How to change ctx.Response.Status value inside the goroutine closure? 回答1: You have no guarantee to observe changes made to the value of a variable in another goroutine without synchronization . See The Go Memory Model for details. So if you want to