简体   繁体   English

在 Intellij 中调试 Go 例程

[英]Debug a Go routine in Intellij

Hi I hava a following Golang program which contain a Go Routine.嗨,我有一个包含 Go 例程的以下 Golang 程序。

   func main() {
    go func(){
        fmt.Println("Break Point 1")
    }()
       fmt.Println("Break Point 2") 
       time.Sleep(100 * time.Second)
       fmt.Println("hello")
}

Now my program doesn't break at breakpoint 1. How do I debug routines also?现在我的程序不会在断点 1 处中断。我还如何调试例程?

func main() {
    go func() {
        fmt.Println("Break Point 1")
    }()
    fmt.Println("Break Point 2")
    time.Sleep(1 * time.Second)
    fmt.Println("hello")
}

It seems like before the go routine gets scheduled the program terminates and that is the reason the breakpoint is not reached.似乎在 go 例程被安排之前程序终止,这就是未到达断点的原因。 Can you try adding sleep in main routine?您可以尝试在主程序中添加睡眠吗?

Ideally you should be using wait groups, which can help you wait until your routine finishes up.理想情况下,您应该使用等待组,它可以帮助您等到例行程序完成。 wg.Wait() in main will ask main to wait until all wait groups are done. wg.Wait() in main 将要求 main 等待所有等待组完成。 in your go function add wg.Add(1) and at the end perform wg.Done().在你的 go 函数中添加 wg.Add(1) 并在最后执行 wg.Done()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM