I create a sample project in Golang:
sampleapp/
sampleapp/main.go
which has the following code:
package main
import (
"flag"
"fmt"
)
func main() {
var name = flag.String("name", "user1", "user name")
var age = flag.Int("age", 20, "user age")
fmt.Println(*name)
fmt.Println(*age)
}
I followed https://code.visualstudio.com/docs/editor/debugging and created the following launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {},
"args": []
},
{
"name": "Launch exec",
"type": "go",
"request": "launch",
"mode": "exec",
"program": "${workspaceFolder}/sampleapp",
"env": {},
"args": []
}
]
}
When I use Launch File debug mode setting breakpoints on main - I am able to single step through the program and can see the values of username and age as follows:
But when I use Launch exec debug mode after building the app like so:
go build
I can single step through the code but the Local section hangs with a spinner going on continuously and doesn't show any local variables as per the following:
I have to do a Reload window to get rid of the hanging Local vars spinner. I checked vscode issues and saw https://github.com/microsoft/vscode-go/issues/2444 but in that case the variables are not shown when there is a panic. But in my case, I am just printing 2 variables.
I thought it might be an issue with my dlv installation but when I debugged using dlv with the executable, I was able to get the values (deleting unnecessary lines from dlv output):
dlv exec sampleapp
Type 'help' for list of commands.
(dlv) break main.main
Breakpoint 1 set at 0x109e1f3 for main.main() ./main.go:8
(dlv) s
> main.main() ./main.go:8 (hits goroutine(1):1 total:1) (PC: 0x109e1f3)
Warning: debugging optimized function
3: import (
4: "flag"
5: "fmt"
6: )
7:
=> 8: func main() {
9: var name = flag.String("name", "user1", "user name")
10: var age = flag.Int("age", 20, "user age")
11: fmt.Println(*name)
12: fmt.Println(*age)
13: }
(dlv) s
> main.main() ./main.go:9 (PC: 0x109e202)
Warning: debugging optimized function
4: "flag"
5: "fmt"
6: )
7:
8: func main() {
=> 9: var name = flag.String("name", "user1", "user name")
10: var age = flag.Int("age", 20, "user age")
11: fmt.Println(*name)
12: fmt.Println(*age)
13: }
(dlv) s
> main.main() ./main.go:11 (PC: 0x109e29f)
Warning: debugging optimized function
6: )
7:
8: func main() {
9: var name = flag.String("name", "user1", "user name")
10: var age = flag.Int("age", 20, "user age")
=> 11: fmt.Println(*name)
12: fmt.Println(*age)
13: }
(dlv) stepout
user1
> main.main() ./main.go:12 (PC: 0x109e31a)
Warning: debugging optimized function
7:
8: func main() {
9: var name = flag.String("name", "user1", "user name")
10: var age = flag.Int("age", 20, "user age")
11: fmt.Println(*name)
=> 12: fmt.Println(*age)
13: }
(dlv) p name
*"user1"
(dlv) p age
*20
Is there something very basic that I am missing here?