Join us
Go is a compiled programming language designed by Robert Griesemer, Rob Pike, and Ken Thompson at google.
It is statically typed language which is syntactically similar to C language but offering additional features like structural typing, garbage collection, memory safety, and CSP-style concurrency.
Diving deep into the GO debugger tool: Delve
Using Delve, you can view, add and change breakpoints in a Go program, navigate the program either line-wise or through breakpoints, inspect variables, functions, and expression values, and finally analyze all the programs in details.
Let us first install the Delve debugger tool.
Downloading and installing Go Delve
Go Delve can be downloaded and installed by using the go get command.
Linux, Windows, OSX
Clone the git repository and build:
$ git clone https://github.com/go-delve/delve
$ cd delve
$ go install github.com/go-delve/delve/cmd/dlv
Alternatively, on Go version 1.16 or later:
# Install the latest release:
$ go install github.com/go-delve/delve/cmd/dlv@latest
# Install at tree head:
$ go install github.com/go-delve/delve/cmd/dlv@master
# Install at a specific version or pseudo-version:
$ go install github.com/go-delve/delve/cmd/dlv@v1.7.3
$ go install github.com/go-delve/delve/cmd/dlv@v1.7.4–0.20211208103735–2f13672765fe
The following elements should already be set up if you have a working Go installation:
-> go env GOBIN
You can view all the environment variables of your GO environment using the “go env” command. You can run the go env command on the command prompt, the following will the output.
$ go env -w GOBIN=$GOPATH/src/github.com/go-delve/delve/delve/cmd/dlv
You have finally set up our environment, and now you are ready to debug the code !!
Following is the code that we will be using for debugging:
package main
import “fmt”
func main() {
var a [5]int
fmt.Println(“emp:”, a)
a[4] = 100
fmt.Println(“set:”, a)
fmt.Println(“get:”, a[4])
fmt.Println(“len:”, len(a))
b := [5]int{1, 2, 3, 4, 5}
fmt.Println(“dcl:”, b)
var twoD [2][3]int
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
twoD[i][j] = i + j
}
}
fmt.Println(“2d: “, twoD)
}
2. Let’s put a break at lines 8, 12, and 25.
3. Type “continue” to see break points in the code in debugging mode.
4.As shown in the above image, you can see there is a breakpoint at the main function.
5. Type “continue” again to see the next breakpoint and type “locals” for viewing local variables declared in the line.
6. If you visit breakpoint at line 12 after typing “continue”, you’ll be able to see the output of above code line 12.
7. After continuing the last line i.e. line 25, you’ll be able to see the following output which states “Process 9576 has exited with status 0” if there is no error in your code.
8.To exit from the test environment type the exit command.
You have finally debugged the code using delve …. 👍
For any further Queries or anything related to Golang Development,Coding, Blogging, Tech Documentation you can DM me on Linkedin or instagram id=acanubhav94.
Join other developers and claim your FAUN account now!
Freelancer
@aniforverizonInfluence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.