在VSCode中,如何使用特定的文件名提供编译命令?

huangapple go评论74阅读模式
英文:

in vscode how to provide compilation command with specific file names

问题

我正在尝试在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

现在我正在尝试调试bug1.go,使用以下launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${workspaceFolder}",
      "env": {},
      "args": []
    }
  ]
}

所以我在VSCode中打开了bug1.go,并按下了运行和调试按钮,然后我看到从bug2.go报告了错误。看起来VSCode正在尝试将目录中的所有文件一起编译,并检测到了代码重复:

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?

英文:

I am trying to debug the following code in vscode. I have 2 versions of the file in the same directory, with minor differences (temp edits during debugging). So I now have 3 files in the directory: go.mod, bug1.go, and 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)
}

I can run the above code from command line like this (which only compiles bug1.go and ignores bug2.go):

go run bug1.go

Now I am trying to debug bug1.go, using the following launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${workspaceFolder}",
      "env": {},
      "args": []
    }
  ]
}

So I opened bug1.go in vscode, and pressed Run and Debug, and then I am seeing errors being reported from bug2.go. Looks like vscode is trying to compile all the files in the directory together, and is detecting code duplications:

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

How do I configure vscode to compile only bug1.go and ignore bug2.go?

答案1

得分: 0

program设置为${file}

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch current file",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${file}"
        }
    ]
}
  • program:在debugtest模式下,指向程序文件夹(或该文件夹中的任何go文件),在exec模式下指向预构建的二进制文件进行调试的路径。如果它不是绝对路径,扩展将将其解释为工作区相对路径。

    (默认值:${workspaceFolder}

    请参阅Launch.json属性

  • ${file}:当前打开的文件

    请参阅变量参考

英文:

Set program to ${file}:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch current file",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${file}"
        }
    ]
}
  • program: Path to the program folder (or any go file within that folder) when in debug or test mode, and to the pre-built binary file to debug in exec mode. If it is not an absolute path, the extension interpretes it as a workspace relative path.

    (Default: "${workspaceFolder}")

    See Launch.json attributes

  • ${file}: the current opened file

    see Variables Reference

huangapple
  • 本文由 发表于 2023年6月30日 13:40:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76586320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定