You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Studio Code中为Golang启用竞态检测器及调试不生效问题处理

Fixing Race Detector Not Working in VS Code Go Debugging

It looks like you’re mixing up build and debug configuration flags in the VS Code Go extension! The go.buildFlags setting only applies to regular build operations (like running go build), but when you’re debugging, the extension uses a separate set of flags for the debug session. That’s why your -race flag isn’t triggering the race detector during debugging.

Here’s how to fix it:

Option 1: Global Debug Flag Setting (settings.json)

Add the -race flag to the go.debugFlags configuration in your user or workspace settings.json. This will apply the race detector to all your Go debugging sessions:

"go.debugFlags": ["-race"]

Option 2: Per-Launch Configuration (launch.json)

If you want to enable the race detector only for specific debugging scenarios, configure it in your launch.json file (found under the Run and Debug tab). Modify your launch configuration to include the debugFlags property:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Current File",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${file}",
            "debugFlags": ["-race"]
        }
    ]
}

Important Note

After updating either configuration, make sure to restart your debugging session for the changes to take effect. Once you do, the race detector should work just like it does with go run -race file.go, and you’ll see the Found 1 data race(s) message in the debug console when a race condition is detected.

If you ever need to enable the race detector for tests, you can use the go.testFlags setting similarly:

"go.testFlags": ["-race"]

内容的提问来源于stack exchange,提问作者Farshid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:17:31