Thursday, April 20, 2017

How to Setup Debugger in Golang with Visual Code



This configuration was implemented on OpenSuse 42.2,  Go1.8.1.linux-amd64, Visual Code, Git and Delve debugger


Install Go1.8.1.linux-amd64
1. Go can be installed by using YaST or you can download from here
2. Setting Environment with terminal
       export GOROOT=/home/addies/golang/go
       export GOPATH=$GOROOT/bin
       export GOBIN=$GOROOT/bin
       export PATH=$PATH:$GOROOT/bin

Test your installation by creating file hello.go and copy paste code below :

        package main

        import "fmt"

        func main() {
           fmt.Printf("hello, world\n")
        }

In your project folder, compile by using
      cd /home/addies/golang/src/
      go build 
      ./hello

Result:
     hello, world



Install Visual Code
1. Install Visual Code
2. Install Go plugin in Visual Code by pressing Ctrl + Shift + X  and type lukehoban. After installed then restart your Visual Code to load the plugin



Install Delve Debugger
Open your terminal and Type command
       go get github.com/derekparker/delve/cmd/dlv

Test installation by opening your hello folder project with visual code
1. Add break point by pressing F9


Pic 1. Add Breakpoint by pressing F9


2. Press Ctrl + Shift + D to enter debug mode
3. Press F5 to run in debug mode then visual code will stop at the break point

Please refer to picture as below for the debugging result:



Pic 2. Debugging result

You can Step Over with F10, Step Into with F11, Step Out with Shift + F11, Toggle Breakpoint with F9, Continue with F5 and add Watch variables.


That's it...

Thank you



Other Topics: