Reputation: 3401
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
Upvotes: 3
Views: 6000
Reputation: 1
Mulitple go files with func main() in the same folder is convinient for simple code snippents.
You cannot use go build on that folder. Go will give an error:
main redeclared in this block
But you can run individual files using command
go run file.go
To run single file in VSCode you have to add Launch file configuration to VSCode
Added new configuration would be:
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}"
},
Upvotes: 0
Reputation: 11
The way this worked for me:
go mod init
inside the package directory to start a module. This way go build
works without specifying the .go fileslaunch.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/"
Upvotes: 0
Reputation: 3401
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": [""]
},
Upvotes: 3
Reputation: 131
Using "program": "." worked for me:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": ".",
"cwd": "${workspaceFolder}",
}
]
}
Upvotes: 9
Reputation: 1897
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.
Upvotes: 0