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

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



Solution 1:[1]

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

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joshua DeMoss
Solution 2 Marcel Kohls