如何在VSCode的launch.json中编译多个Go文件?

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

How to compile multiple go files in vscode launch.json?

问题

如果我有几个需要在主包中编译的go文件,我该如何在launch.json中指定它们?我想将它们重构为包,但是这个项目不允许。

例如,要在命令行上运行它们,我必须使用:

go run main.go stuff.go other.go

我该如何在launch.json文件中包含它们?

{
    "name": "Launch myprog",
    "type": "go",
    "request": "launch",
    "mode": "debug",
    "program": "${workspaceRoot}/cmd/myprog/main.go",
    "args": ["param"]
}

我已经尝试了一些明显的方法。我想进行一些调试。

那么...如何在launch.json中指定编译文件夹而不仅仅是一个特定的文件?

如果go run main.go等同于"program": "${workspaceRoot}/cmd/myprog/main.go"

那么,go run .等同于什么?"program": "${workspaceRoot}/cmd/myprog/[?????]"

因为明显的方法对我不起作用。

谢谢。

英文:

If I have several go files that need to be in the main package, how can I specify them to be compiled in the launch.json? I would refactor them into packages. But this project is resisting.

i.e. To run them on the command line, I have to use:

go run main.go stuff.go other.go

How would I include that in the launch.json file?

{
    "name": "Launch myprog",
    "type": "go",
    "request": "launch",
    "mode": "debug",
    "program": "${workspaceRoot}/cmd/myprog/main.go",
     "args": ["param"]
},

I've tried the obvious ways. I would like to do some debugging.

So... How do I specify in launch.json to compile the package in the folder and not just one specific file?

If go run main.go is equivalent to "program": "${workspaceRoot}/cmd/myprog/main.go"

Then, go run . is equivalent to what? "program": "${workspaceRoot}/cmd/myprog/[?????]"

Because the obvious didn't work for me.

Thanks

答案1

得分: 5

使用"program": "." 对我有用:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "启动包",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": ".",
      "cwd": "${workspaceFolder}",
    }
  ]
}
英文:

Using "program": "." worked for me:

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

答案2

得分: 2

我的中文翻译如下:

我的系统可能出现了其他问题。因为在某个时候,一切都突然开始正常工作了...不过我现在也在使用插件golang.go-nightly的夜间版本。它完美地修复了缺失的根调试功能...

现在这个工作得很好...

{
   "name": "Prog",
   "type": "go",
   "request": "launch",
   "mode": "debug",
   "program": "${workspaceRoot}/src/cmd/prog/",
   "args": [""]
}
英文:

There must have been some other problem with my system. Because at some point it all just started working... Though I am also now using the nightly version of the plugin golang.go-nightly. It fixed the missing root debug feature beautifully...

This now works fine...

{
   "name": "Prog",
   "type": "go",
   "request": "launch",
   "mode": "debug",
   "program": "${workspaceRoot}/src/cmd/prog/",
   "args": [""]
},

答案3

得分: 0

我相信你在问题中混淆了一些概念,让我逐步来回答:

  • vscode 中,launch.json 文件是由 vscode 自动创建的,用于帮助进行调试,它不是 Go 语言的一部分,也不定义应用程序的运行方式或编译方式。更多参考信息,请查看如何使用 Visual Studio Code 调试 Go 代码

  • 编译应用程序(go build)意味着将与特定包相关的所有文件转换为二进制文件。请参阅这篇文章,了解如何将包链接到应用程序编译中。

  • 运行应用程序可以使用 go run 命令,它的效果几乎与编译相同,只是该命令不会生成最终的可共享二进制文件。请查看go run vs go build

现在,关于你的问题,如果你需要在 vscode 中启用适当的调试功能,请查看gopls-vscode,但如果你的代码由于依赖其他文件/包而无法编译,请参考上面提到的关于包的教程。

希望我能给你一些帮助。

英文:

I believe you are mixing some concepts in your question, so let me try to answer step by step:

  • In vscode the launch.json file is created automatically by vscode to help it on debugging, it is not part of the go language and it does not define how your application will run or will be compiled. For more reference take a look at How To Debug Go Code with Visual Studio Code.

  • Compile your application (go build) means that all the files related to a specific package will be transformed in a binary. Take a look in this article to understand how the packages are linked to your application compile.

  • Run your application can be done using go run which will have almost the same effect as compiling except that this command does not generate your final shareable binary file. Check go run vs go build

Now, regarding to your question, if you need to enable the proper debugging on vscode, take a look on gopls-vscode, but if your code is not compiling because of dependencies on other files/packages then take a look on the tutorial about packages mentioned above.

I hope that I can give you some help.

答案4

得分: 0

这是我的翻译结果:

我使用的方法是:

  1. 在包目录中运行 go mod init 来启动一个模块。这样,go build 就可以在不指定 .go 文件的情况下工作。
  2. 在项目的 launch.json 中,将 program 字段的值设置为包的根目录,不需要指定任何源文件,例如在你的情况下,它应该是 "program": "${workspaceRoot}/cmd/myprog/"
英文:

The way this worked for me:

  1. Run go mod init inside the package directory to start a module. This way go build works without specifying the .go files
  2. In the launch.json for your project, set the value of program field to the root directory of the package, without specifying any source file i.e. in your case, it would be "program": "${workspaceRoot}/cmd/myprog/"

huangapple
  • 本文由 发表于 2021年11月23日 02:39:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/70070830.html
匿名

发表评论

匿名网友

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

确定