How to Debug Go

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


Debugging is an essential part of the development process, but it can be a daunting task for beginners. In this article, we will provide you with practical tips and tricks on how to debug Go code like a pro. We will cover topics such as using print statements, setting breakpoints, inspecting variables, and more.
Debugging is an essential part of the development process, but it can be a daunting task for beginners. In this article, we will provide you with practical tips and tricks on how to debug Go code like a pro. We will cover topics such as using print statements, setting breakpoints, inspecting variables, and more.

  1. Using Print Statements
    One of the most basic yet effective ways to debug your Go code is to use print statements. Print statements allow you to output information to the console or terminal window while the program is running. This can be useful for tracking down bugs, checking variable values, and verifying that your code is executing as expected.
    To use a print statement in Go, simply type fmt.Println(“Hello World!") at the point in your code where you want to output information. For example:
    package main
    import “fmt”
    func main() {
    fmt.Println(“Welcome to my first Go program.")
    }
    When you run this program, it will print out the message “Welcome to my first Go program.” in your terminal window.
  2. Setting Breakpoints
    Breakpoints are a powerful debugging tool that allow you to pause your code at specific points and inspect variables or step through lines of code. To set a breakpoint in Go, simply click on the left margin of your code editor next to the line number where you want to stop execution. This will insert a red circle into the margin, indicating that this is a breakpoint.
    Now when you run your program, it will pause execution at the point where you set the breakpoint. You can then use various commands in the debugger to continue running the code, inspect variables, and step through lines of code.
  3. Inspecting Variables
    When debugging Go code, it’s often useful to inspect the values of variables. To do this, simply hover your mouse over a variable name or select it and press Ctrl + Shift + I (or Command + Shift + I on a Mac). This will open up a tooltip that displays information about the variable, including its type, value, and other details.
    For example, if you had the following code:
    package main
    import “fmt”
    func main() {
    var greeting string = “Hello World!”
    fmt.Println(greeting)
    }
    When you hover your mouse over the variable greeting in your code editor, a tooltip will display the type and value of the variable:
  4. Stepping Through Code
    Sometimes you may want to step through your Go code line by line to see exactly what it’s doing. To do this, simply press F10 (or Command + ; on a Mac) when execution is paused at a breakpoint. This will execute the current line of code and then pause again at the next line in the program.
    By stepping through your code line by line, you can gain a better understanding of what’s happening under the hood and where things might be going wrong.
  5. Using Debugger Tools
    Go provides a number of built-in tools for debugging your code, including the Go debugger and the Delve debugger. These tools provide a range of features for inspecting variables, setting breakpoints, and stepping through code.
    To use these tools, simply run your program with the flag –debug or -d, followed by any additional arguments you want to pass to the debugger. For example:
    go build main.go && ./main –debug
    This will compile your program and launch it in debug mode, allowing you to use the debugger’s features to step through code, inspect variables, and set breakpoints.
  6. Testing Your Code
    When debugging Go code, it’s often useful to write tests for your code as well. This can help you catch bugs early on and make sure that your program is functioning correctly.
    To write tests in Go, simply create a new file with the same name as your program but with “_test” appended to the end. For example:
    package main_test
    import “testing”
    func TestHelloWorld(t *testing.T) {
    var greeting string = “Hello World!”
    fmt.Println(greeting)
    }
    This will create a new test file that can be used to write tests for your program. You can then use the go test command to run these tests and verify that everything is working correctly. For example:
    go test main_test.go
  7. Conclusion
    Debugging Go code can be a challenging task, but with practice and experience, you’ll become more comfortable using print statements, setting breakpoints, inspecting variables, stepping through code, and other debugging tools. Remember to use testing to catch bugs early on and make sure your program is functioning correctly. With these tips and tricks under your belt, you’ll be well on your way to becoming a master Go programmer.

Questions or comments? Reach out to me


Learn how to leverage the Go Standard Library like a PRO.

I just created a new course, The Go Standard Library, check it out!