Install the official Go extension in VS Code and Delve debugger using go install github.com/go-delve/delve/cmd/dlv@latest, ensuring dlv is in your PATH. 2. Create a launch.json file in the .vscode folder with configurations such as "mode": "auto" and "program": "${workspaceFolder}" for launching the main package, or use "mode": "test" to debug tests. 3. Open the Run and Debug view, select a configuration, set breakpoints by clicking beside line numbers, and start debugging with F5 to pause at breakpoints and inspect variables, stack, and goroutines. 4. For common issues, verify Delve is built with the correct Go version, avoid compiler optimizations hiding variables, and use "mode": "remote" with headless Delve for remote debugging. 5. With proper setup, VS Code provides a seamless debugging experience for Go, eliminating reliance on print statements.
Debugging Go applications in VS Code is straightforward once you’ve set up the environment correctly. With the right extensions and configuration, you can set breakpoints, inspect variables, and step through code just like in other major languages.

Install the Go Extension and Delve Debugger
First, make sure you have the official Go extension for VS Code installed. It provides rich support for Go, including debugging. You can find it in the Extensions marketplace (Ctrl Shift X
) and search for "Go" by Go Team at Google.
Under the hood, the Go extension uses Delve (dlv) for debugging. Install Delve by running:

go install github.com/go-delve/delve/cmd/dlv@latest
Make sure dlv
is in your PATH
. You can verify by running:
dlv version
Configure Launch Settings for Debugging
VS Code uses a launch.json
file to define debug configurations. Create one in your project under the .vscode
folder:

{ "version": "0.2.0", "configurations": [ { "name": "Launch package", "type": "go", "request": "launch", "mode": "auto", "program": "${workspaceFolder}" } ] }
Here’s what the key fields mean:
mode: "auto"
lets VS Code choose the best way to launch (e.g.,debug
for local, orremote
if needed).program
points to the directory containing yourmain
package. You can also point to a specific file like"${workspaceFolder}/cmd/api"
.
Other common configurations:
Debug a specific test:
{ "name": "Debug Test", "type": "go", "request": "launch", "mode": "test", "program": "${workspaceFolder}" }
Debug a single file:
{ "name": "Run main.go", "type": "go", "request": "launch", "mode": "auto", "program": "${workspaceFolder}/main.go" }
Start Debugging
Once configured:
- Open the Run and Debug view (
Ctrl Shift D
). - Select your configuration (e.g., "Launch package").
- Set breakpoints by clicking to the left of line numbers in your Go file.
- Press F5 or click Start Debugging.
The debugger will:
- Build your program with debug symbols.
- Launch it under Delve.
- Pause at breakpoints.
- Let you inspect variables, call stack, and goroutines in the sidebar.
You can:
- Step over (
F10
) - Step into (
F11
) - Continue (
F5
) - View local variables and watch expressions
Common Issues and Tips
- Breakpoints not hitting? Make sure you're not using
CGO_ENABLED=0
in environments where it's required, or ensure Delve is properly built with the same Go version. - Optimizations interfering? The Go extension usually handles this, but if variables seem missing, it’s because the compiler optimized them out — Delve can’t always recover them.
- Debugging tests: Use
"mode": "test"
and optionally specify"args"
to filter tests:"args": ["-test.run", "TestMyFunction"]
-
Remote debugging: If debugging a Go app running on a server, use
"mode": "remote"
and startdlv
on the server withdlv exec ./myapp --headless --listen=:2345
.
Basically, once Delve and the Go extension are set up, debugging in VS Code feels natural and powerful — no need to rely solely on fmt.Println
anymore.
The above is the detailed content of Debugging Go applications in VS Code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go's switch statement will not be executed throughout the process by default and will automatically exit after matching the first condition. 1. Switch starts with a keyword and can carry one or no value; 2. Case matches from top to bottom in order, only the first match is run; 3. Multiple conditions can be listed by commas to match the same case; 4. There is no need to manually add break, but can be forced through; 5.default is used for unmatched cases, usually placed at the end.

In Go, to break out of nested loops, you should use labeled break statements or return through functions; 1. Use labeled break: Place the tag before the outer loop, such as OuterLoop:for{...}, use breakOuterLoop in the inner loop to directly exit the outer loop; 2. Put the nested loop into the function, and return in advance when the conditions are met, thereby terminating all loops; 3. Avoid using flag variables or goto, the former is lengthy and easy to make mistakes, and the latter is not recommended; the correct way is that the tag must be before the loop rather than after it, which is the idiomatic way to break out of multi-layer loops in Go.

Usecontexttopropagatecancellationanddeadlinesacrossgoroutines,enablingcooperativecancellationinHTTPservers,backgroundtasks,andchainedcalls.2.Withcontext.WithCancel(),createacancellablecontextandcallcancel()tosignaltermination,alwaysdeferringcancel()t

InstallDockerDesktop,VSCode,andtheofficialDockerextensionfromMicrosoft.2.CreateaDockerfileinyourprojectroot,suchasusingnode:18-alpineforNode.jsappswithproperCOPY,RUN,andCMDinstructions.3.UsetheDockerextensionpaneltobuildtheimage,thenrunitasacontainer

VSCode extensions are add-ons for customizing and enhancing coding environments. They help developers work faster, smarter and more comfortable by providing better syntax highlighting, code suggestions, and tools for specific languages and frameworks. Extends an editor-like app plug-in to add new features or improve existing features. Some are from Microsoft, but most are built by community or third-party developers, covering a wide range of features such as JavaScript debugging, Markdown preview, and more. Common types include: 1. Language support (such as Python, Rust); 2. Inspector and formatting tools; 3. Git integration; 4. UI themes and icon packages; 5. Code snippets and automatic completion tools. The reason for using extensions is that each developer has different habits

Use a dedicated and reasonably configured HTTP client to set timeout and connection pools to improve performance and resource utilization; 2. Implement a retry mechanism with exponential backoff and jitter, only retry for 5xx, network errors and 429 status codes, and comply with Retry-After headers; 3. Use caches for static data such as user information (such as sync.Map or Redis), set reasonable TTL to avoid repeated requests; 4. Use semaphore or rate.Limiter to limit concurrency and request rates to prevent current limit or blocking; 5. Encapsulate the API as an interface to facilitate testing, mocking, and adding logs, tracking and other middleware; 6. Monitor request duration, error rate, status code and retry times through structured logs and indicators, combined with Op

Use the template.ParseFS and embed package to compile HTML templates into binary files. 1. Import the embed package and embed the template file into the embed.FS variable with //go:embedtemplates/.html; 2. Call template.Must(template.ParseFS(templateFS,"templates/.html")))) to parse all matching template files; 3. Render the specified in the HTTP processor through tmpl.ExecuteTemplate(w,"home.html", nil)

To correctly copy slices in Go, you must create a new underlying array instead of directly assigning values; 1. Use make and copy functions: dst:=make([]T,len(src));copy(dst,src); 2. Use append and nil slices: dst:=append([]T(nil),src...); both methods can realize element-level copying, avoid sharing the underlying array, and ensure that modifications do not affect each other. Direct assignment of dst=src will cause both to refer to the same array and are not real copying.
