我正在try 调试vscode中的以下代码.我在同一个目录中有两个版本的文件,有微小的差异(调试期间的临时编辑).因此,我现在的目录中有3个文件:go.mod、bug1.go和bug2.go.
//bug1.go
package main
import (
"fmt"
)
type Animal int64
const (
Goat Animal = iota
Cat
)
func (n Animal) String() string {
switch n {
case Goat:
return "Goat"
case Cat:
return "Cat"
}
return "?"
}
type Group struct {
A, B Animal
}
func main() {
fmt.Println("Animal: ", Cat)
}
我可以从命令行运行上面的代码,如下所示(它只编译bug1.go而忽略bug2.go):
go run bug1.go
现在,我正在try 使用以下启动文件.json调试bug1.go:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"env": {},
"args": []
}
]
}
因此,我在vscode中打开bug1.go,然后按Run和Debug,然后我看到从bug2.go报告的错误.看起来vscode正在try 将目录中的所有文件编译在一起,并正在检测代码重复:
Build Error: go build -o /Users/.../bug/__debug_bin -gcflags all=-N -l .
./bug2.go:9:6: Animal redeclared in this block
./bug1.go:9:6: other declaration of Animal
./bug2.go:12:5: Goat redeclared in this block
./bug1.go:12:5: other declaration of Goat
如何将vscode配置为只编译bug1.go而忽略bug2.go?