我使用过PHP和Python之类的语言,但是最近我维护了一个Go项目,该项目使用了多个worker和goroutines。
在PHP或Python中,我们可以将代码的执行理解为所有同步调用。但是我发现很难理解Golang中代码的执行方式,尤其是在worker和goroutine中。谁能建议我使用任何工具来获取执行流程(被调用函数的名称)?
例如,如果有一个其中包含各种goroutine的API,并且在这些goroutine中,则将调用其他goroutine,因此如何知道以什么顺序调用这些函数。
由于我不熟悉Go,如果答案太明显,请忽略。
谢谢
[[i] f有一个API,其中存在各种goroutine,而在这些goroutine中,则称为其他goroutine,[...]
没有。
Go运行时跟踪工具将帮助您跟踪,但是您需要在go代码开始中添加跟踪选项
package main
import (
"os"
"runtime/trace"
)
func main() {
trace.Start(os.Stderr) //start the trace
defer trace.Stop() // defer to the end
.... //rest of the code
}
然后使用,
go run main.go 2> trace.out
then use the trace tool
go tool trace trace.out.
此链接具有更多信息https://blog.gopheracademy.com/advent-2017/go-execution-tracer/