15

consider the small snippet below:

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()
}

in delve, we can easily switch between goroutines using the commands like

goroutine
goroutine <id>
goroutine <id> <command>

and if i want run step by step in goroutine 1, just use the command

goroutine 1 next

In vscode, it seems the only way to deal with goroutines is the call stack, however, it seems that this is the internal threads in go runtime, not the goroutines, so how can i focus the running process in the specified goroutine?

newbieee
  • 193
  • 1
  • 4

2 Answers2

2

so how can i focus the running process in the specified goroutine?

vscode-go issue 1797 should bring a possible solution:

For example, when debugging a simple hello world program, 5 different goroutines appear in the callstack:

Program stopped on a breakpoint in main.go:

This proposal is to (by default) only show the user goroutines in the call stack. When using this new default, the call stack for hello world will only show a single goroutine, as would be expected from a simple program with no concurrency:

unique goroutine

It just (Oct. 2021) has been submitted in CL 359402 (CL = Change List: a set of commits/patches)

package.json: add config to hide system goroutines in debug

This change includes the configuration for hiding the system goroutines in a debug session

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-4

Jetbrains have introduced a cool new feature for debugging go-routines in its IDE. Please follow this link to take a look

It uses feature of labels provided by pprof and is not exclusive to jetbrains I suppose although they might have tweaked it for usability. Here is an article on lables so that you can try to implement it in vscode also.