How to setup Visual Studio Code to debug GoLang with Delve
People who have worked with GoLang might already know how hard it is to debug GoLang. Without a proper tool it would be an nightmare when developing and debugging applications using GoLang. But now the hustle is over. Now there are tools that we can overcome those issues. Previously I used SublimeText for Go development but when I found this I switched to VSCode (Visual Studio Code). Therefore I will be using VSCode for debugging GoLang for this post.
For me this didn’t work the first time I tried debugging Golang on VSCode. After many attempts I was finally able to debug my application on it.
Following are the set of instructions which you can follow to get this working.
- Install GoLang if you haven’t already go it installed.
- Install Visual Studio Code if you still don’t have it.
- Install Go for VSCode package which once installed gives support to GoLang.
Ctrl + Shift + P.
ext install& select
goand select Go plugin made by lukehoben.
- Install Delve the golang debugger. (please refer bellow for more installation guidance)
Follow the above instructions and get them installed.
For Delve, I was unable to complete the installation due to the lack of information on their installation guidance documentation. After researching for a while I was able to find a solution how to get it installed.
You can use this method to install Delve.
$go get -u github.com/derekparker/delve/cmd/dlv
- Linux users, that’s it for you. It doesn’t support. (at the time of writing)
- Mac users can follow this method here.
Once the Delve is installed, it will build an executable depending on the system you are on. Make sure the compiled executable (dlv.exe) is in the PATH environment variable.
Once that is done, open up the project directory in Visual Studio Code IDE. You can follow the instructions and install all the missing Go helpers. If you like the manual method, here are all the packages you need to install.
Go helpers for VSCode
go get -u -v github.com/nsf/gocode go get -u -v github.com/rogpeppe/godef go get -u -v github.com/golang/lint/golint go get -u -v github.com/lukehoban/go-outline go get -u -v sourcegraph.com/sqs/goreturns go get -u -v golang.org/x/tools/cmd/gorename go get -u -v github.com/tpng/gopkgs go get -u -v github.com/newhook/go-symbols go get -u -v golang.org/x/tools/cmd/guru
Make sure the main function is located in the root of the project directory. If you have multiple main functions, you can use “// +build ignore” at the top of the file before the package name. Click here to learn more about GoLang Build constrains.
If you have any questions about this, please feel free to contact me! I would love you help 🙂