草庐IT

GoRoutine

全部标签

go - 为什么这个程序中存在竞争条件?

我正在查看typicaldataraces在Golang文档中,我不太明白为什么这个程序有问题:funcmain(){varwgsync.WaitGroupwg.Add(5)fori:=0;i它打印5,5,5,5,5而我希望它打印0,1,2,3,4(不一定按这个顺序).我的看法是,当goroutine在循环内创建时,i的值是已知的(例如,可以执行log.Println(i)在循环的开头并查看预期值)。所以我希望goroutine在创建时捕获i的值并在以后使用它。显然这不是发生了什么,而是为什么? 最佳答案 您的函数字面量从外部作用域

go - 链式函数如何作为 goroutines 执行?

给定thisplayground:packagemainimport"fmt"funcmain(){gooneFunc().anotherFunc()}funconeFunc()something{fmt.Println("oneFunc")returnsomething{}}typesomethingstruct{}func(ssomething)anotherFunc(){fmt.Println("anotherFunc")}为什么输出是:oneFunc并且永远不会打印“anotherFunc”? 最佳答案 anotherFun

go - 链式函数如何作为 goroutines 执行?

给定thisplayground:packagemainimport"fmt"funcmain(){gooneFunc().anotherFunc()}funconeFunc()something{fmt.Println("oneFunc")returnsomething{}}typesomethingstruct{}func(ssomething)anotherFunc(){fmt.Println("anotherFunc")}为什么输出是:oneFunc并且永远不会打印“anotherFunc”? 最佳答案 anotherFun

go - 从并发 goroutines 设置和访问指针

我有一个map,由goroutineA使用并在goroutineB中一次替换一次。替换我的意思是:varamap[T]N//usesthemapfuncgoroutineA(){for(...){tempA=a..usestempAinsomeway...}}//refreshesthemapfuncgorountineB(){for(...){time.Sleep(10*time.Seconds)otherTempA=make(map[T]N)...initializesothertempA....a=otherTempA}}你看到这段伪代码有什么问题了吗?(在并发方面)

go - 从并发 goroutines 设置和访问指针

我有一个map,由goroutineA使用并在goroutineB中一次替换一次。替换我的意思是:varamap[T]N//usesthemapfuncgoroutineA(){for(...){tempA=a..usestempAinsomeway...}}//refreshesthemapfuncgorountineB(){for(...){time.Sleep(10*time.Seconds)otherTempA=make(map[T]N)...initializesothertempA....a=otherTempA}}你看到这段伪代码有什么问题了吗?(在并发方面)

recursion - 将自身称为 goroutine 的 golang 递归函数无法按预期工作

这个问题在这里已经有了答案:Nooutputfromgoroutine(3个答案)关闭6年前。这个递归函数按预期工作(返回5行数字5到1):packagemainimport("fmt")funcrecur(iterint){ifiter这个没有(只返回1行数字5):packagemainimport("fmt")funcrecur(iterint){ifiter不同的是,在第二种实现中,function调用自己作为一个goroutine。(行gorecur(iter-1))那么有人可以解释这种行为吗?

recursion - 将自身称为 goroutine 的 golang 递归函数无法按预期工作

这个问题在这里已经有了答案:Nooutputfromgoroutine(3个答案)关闭6年前。这个递归函数按预期工作(返回5行数字5到1):packagemainimport("fmt")funcrecur(iterint){ifiter这个没有(只返回1行数字5):packagemainimport("fmt")funcrecur(iterint){ifiter不同的是,在第二种实现中,function调用自己作为一个goroutine。(行gorecur(iter-1))那么有人可以解释这种行为吗?

go - 运行时 : goroutine stack exceeds 1000000000-byte limit, fatal error :打印嵌套结构时堆栈溢出

我有一个嵌套结构。typeConfigOnestruct{//Daemonsectionfromconfigfile.Daemondaemon}typedaemonstruct{LoglevelintLogfilestring}我在该类型上有一个String()string方法,我试图将嵌套的结构元素返回为func(cConfigOne)String()string{returnfmt.Sprintf("%+v\n",c)}当我尝试将其打印为c:=&modules.ConfigOne{}c.Daemon.Loglevel=1c.Daemon.Logfile="/tmp/test.log

go - 运行时 : goroutine stack exceeds 1000000000-byte limit, fatal error :打印嵌套结构时堆栈溢出

我有一个嵌套结构。typeConfigOnestruct{//Daemonsectionfromconfigfile.Daemondaemon}typedaemonstruct{LoglevelintLogfilestring}我在该类型上有一个String()string方法,我试图将嵌套的结构元素返回为func(cConfigOne)String()string{returnfmt.Sprintf("%+v\n",c)}当我尝试将其打印为c:=&modules.ConfigOne{}c.Daemon.Loglevel=1c.Daemon.Logfile="/tmp/test.log

go - 没有取消传播的上下文

我如何创建一个Go上下文的副本(如果你愿意的话,一个克隆),它包含存储在原始文件中的所有值,但不会在原始文件被取消时被取消?对我来说,这确实是一个有效的用例。假设我有一个http请求,它的上下文在响应返回给客户端后被取消,我需要在这个请求结束时在一个单独的goroutine中运行一个异步任务,这个goroutine很可能比父上下文活得更久。funcHandler(ctxcontext.Context)(interface{},error){result:=doStuff(ctx)newContext:=howDoICloneYou(ctx)gofunc(){doSomethingEls