Home >Backend Development >Golang >How to create a debug configuration for a specific .go file in VSCode?
When using VSCode for Go language development, we often need to debug to solve problems. However, for specific .go files, we may need to create custom debug configurations. So, how do you create a debug configuration for a specific .go file in VSCode? In this article, PHP editor Youzi will introduce you to the specific steps and precautions in detail to help you achieve this goal easily. Whether you are a beginner or an experienced developer, this article will provide you with valuable guidance. let's start!
Currently, you must first focus on the .go
file in the editor for debugging, and then press the F5 key to start debugging.
What I want is to be able to press F5 to start debugging, but not have to focus on main.go
in the editor first.
This is the inventory configuration generated by VSCode. You must pay attention to the .go
file before F5:
{ "name": "Launch Package", "type": "go", "request": "launch", "mode": "auto", "program": "${fileDirname}", }
Try specifying program
and cwd
to satisfy it:
{ "name": "Launch Package", "type": "go", "request": "launch", "mode": "auto", "program": "${workspaceFolder}/cmd/sym_dump/main.go", "cwd": "${workspaceFolder}/cmd/sym_dump", }
But this doesn't work, it complains about function not found:
Starting: C:\Users\aybe\go\bin\dlv.exe dap --listen=127.0.0.1:49541 from C:\GitHub\psx_mnd_sym/cmd/sym_dump DAP server listening at: 127.0.0.1:49541 Build Error: go build -o C:\GitHub\psx_mnd_sym\cmd\sym_dump\__debug_bin2780105186.exe -gcflags all=-N -l .\main.go # command-line-arguments .\main.go:340:13: undefined: dumpTypes .\main.go:344:14: undefined: dumpSourceFiles .\main.go:348:14: undefined: dumpDecls .\main.go:357:13: undefined: dumpTypes .\main.go:365:13: undefined: dumpIDAScripts .\main.go:380:13: undefined: dumpTypes (exit status 1)
The actual function is located in another .go
file in the same directory as program
.
Both files have package main
specified at the top of them.
In this case, how do I create a debug configuration for a specific .go
file?
Specify the program
attribute as the directory:
{ "version": "0.2.0", "configurations": [ { "name": "Launch Package", "type": "go", "request": "launch", "mode": "auto", "program": "${workspaceFolder}/cmd/sym_dump" } ] }
${fileDirname}
is The folder path of the currently opened file. There is a reason why the default Launch Package
template uses ${fileDirname}
instead of ${file}
(used by the Launch file
template) .
Let's take a look at Documentation First of all dlv debug
:
There is not much information about package
. Let's turn to the doc
of go run:
If we specify the program
attribute as ${workspaceFolder}/cmd/sym_dump/main.go
, it means that the package contains a single file (main.go
). But the thing is that the package contains another file that defines dumpTypes
, dumpSourceFiles
, etc. That's why it fails to build the program.
By the way, since "if cwd is omitted or empty, the program folder will be used as the working directory" (see Launch.json properties ), you don't need to set cwd
Properties.
The above is the detailed content of How to create a debug configuration for a specific .go file in VSCode?. For more information, please follow other related articles on the PHP Chinese website!