Skip to content

Debug in Go

This document introduces 3 simple methods to debug Go code.

  1. printf, which allows you to format the numbers, variables, and strings;
  2. print, which only prints the string passed as an argument; and
  3. println, which does the same thing as Print, then adds a new line character (\n) at the end of the string passed to it.
  4. log.Print, which prints the string passed as an argument to the standard output stream.

third-party logging frameworks

  • https://github.com/golang/glog
  • https://github.com/Sirupsen/logrus

gdb

https://go.dev/doc/gdb

When you compile and link your Go programs with the gc toolchain on Linux, macOS, FreeBSD or NetBSD, the resulting binaries contain DWARFv4 debugging information that recent versions (≥7.5) of the GDB debugger can use to inspect a live process or a core dump.

```bash
go build -gcflags=all="-N -l"
```

Delve

https://github.com/go-delve/delve

Delve is a debugger for the Go programming language.

```bash
go get -u github.com/go-delve/delve/cmd/dlv
dlv debug github.com/me/foo/cmd/foo
```

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback: dig +short txt issue.imzye.com
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Buy Me a Coffee