goroutine

How to change external variable's value inside a goroutine closure

你说的曾经没有我的故事 提交于 2020-12-30 04:10:00
问题 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

How to change external variable's value inside a goroutine closure

若如初见. 提交于 2020-12-30 04:01:29
问题 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

Simple goroutine not working on Windows

对着背影说爱祢 提交于 2020-08-19 06:30:07
问题 I'm doing some tests with goroutines just to learn how they work, however it seems they are not running at all. I've done a very simple test: package main import ( "fmt" ) func test() { fmt.Println("test") } func main() { go test() } I would expect this to print "test" however it simply doesn't do anything, no message but no error either. I've also tried adding a for {} at the end of the program to give the goroutine time to print something but that didn't help. Any idea what could be the

Simple goroutine not working on Windows

痴心易碎 提交于 2020-08-19 06:29:09
问题 I'm doing some tests with goroutines just to learn how they work, however it seems they are not running at all. I've done a very simple test: package main import ( "fmt" ) func test() { fmt.Println("test") } func main() { go test() } I would expect this to print "test" however it simply doesn't do anything, no message but no error either. I've also tried adding a for {} at the end of the program to give the goroutine time to print something but that didn't help. Any idea what could be the