如何在 vscode 中调试 goroutine?

new*_*eee 6 go goroutine visual-studio-code delve

考虑下面的小片段:

package main

import (
    "fmt"
    "sync"
)

func main() {
    var wg sync.WaitGroup
    wg.Add(2)
    go func() {
        defer wg.Done()
        for i := 1; i < 100; i++ {
            fmt.Println("A:", i)
        }
    }()
    go func() {
        defer wg.Done()
        for i := 1; i < 100; i++ {
            fmt.Println("B:", i)
        }
    }()
    wg.Wait()
}

Run Code Online (Sandbox Code Playgroud)

在 delve 中,我们可以使用以下命令轻松地在 goroutine 之间切换

goroutine
goroutine <id>
goroutine <id> <command>
Run Code Online (Sandbox Code Playgroud)

如果我想在 goroutine 1 中逐步运行,只需使用命令

goroutine 1 next
Run Code Online (Sandbox Code Playgroud)

在vscode中,似乎处理goroutines的唯一方法是调用堆栈,但是,这似乎是go runtime的内部线程,而不是goroutines,那么如何将运行过程集中在指定的goroutine中呢?

小智 -4

Jetbrains 推出了一项很酷的新功能,用于在其 IDE 中调试 go 例程。请点击此链接查看

它使用 pprof 提供的标签功能,并且我认为并不是 jetbrains 独有的,尽管他们可能对其可用性进行了调整。这里有一篇关于标签的文章,你也可以尝试在 vscode 中实现它。