目前vscode里调试go利用的是delve工具,最新delve仅支持调试go1.14及以上版本的代码,但有时有需要用较低版本golang,不方便升级golang。
解决方案之一是在launch.json文件中添加"dlvFlags"项:"--check-go-version=false" (参考https://github.com/golang/vscode-go/blob/master/docs/debugging.md#launch-configurations)
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}",
"dlvFlags": [
"--check-go-version=false"
]
}
]
}
方案一不太好调试test函数,解决方案之二为安装较旧版本的delve,在https://github.com/go-delve/delve/blob/master/CHANGELOG.md 可以检索到各版本golang语言的支持记录,如delve是在v1.3.0添加go1.12的支持的。因此可以安装v.1.3.0版本的delve.
GO111MODULE=on go get github.com/go-delve/delve/cmd/dlv@v1.3.0