Debug in Go
This document introduces 3 simple methods to debug Go code.
print & log
printf
, which allows you to format the numbers, variables, and strings;print
, which only prints the string passed as an argument; andprintln
, which does the same thing as Print, then adds a new line character (\n) at the end of the string passed to it.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
- License under
CC BY-NC 4.0
- Copyright issue feedback
me#imzye.me
, replace # with @ - Not all the commands and scripts are tested in production environment, use at your own risk
- No privacy information is collected here