Can anyone tell me how can I run and use debugger in Visual Studio for a simple go program in Windows step by step?
I took a reference from this page:
https://marketplace.visualstudio.com/items?itemName=lukehoban.Go
But I was not able to run the code. My current GOPATH is C:\dev\go. Please let me know if any other details are required.
First of all, debuggers are not part of Go. C#, F# and other managed languages under the Microsoft stack have debuggers as that's part of the .NET stack.
Second, Visual Studio Code != Visual Studio. VS Code is a light(er) weight IDE that is geared towards extensibility to support a wide range of languages by creating runners. But that's the thing: someone else needs to write the runners and hopefully they created a seamless experience with a Debugger (if available). This is why you have multiple versions of language runners.
In other words: if you want a VSCode-compatible Debugger+Runner for X language, read up on X language about how to debug it.
Go is no exception. You must read the language spec, and specifically I recommend Effective Go as it explains why you don't need a Debugger.
--
Now with all of that said, the community has come together and created somewhat of a debugger for GoLang. It is called Delve.
Learning how to install it for VS Code is beyond this post. I recommend finding a VSCode package that supports Go coding with Delve (there is at least one out there, as I have used it).
Opinion: it's an Ok experience in VSCode to debug Go. I've experimented with it. While visually pleasing, I went back to Atom for it's large package support of many other Go utilities and Linters - most of which is missing in VSCode (and some packages didn't allow me to modify the config to exclude certain Go workflows).
EDIT 2018: After a few years, VSCode has matured nicely! I've since switched 100% to VSCode as my primary editor.
One possible option to install Debugger for Go language on Windows is:
go get github.com/derekparker/delve/cmd/dlv
After that the Visual Studio Code (vscode) will be able to run (debug) launch configuration.
By default, file launch.json
points to the root of the project:
"program": "${workspaceRoot}"
If you want configure it to the another location (eg. your/package/foo/dir) then edit it as the following:
"program": "${workspaceRoot}/foo/dir"
In case you want to debug a Go module running inside an application server you can have a look at debugging golang appengine module with visual studio code.
Explanations are for Appengine server, but you can easily understand how to do remote debugging from vscode using Delve.