xyfkgf
(xyfkgf)
July 25, 2026, 5:34am
1
i want to place print debug messages in my code but i only want them to be compiled in if debug symbols are included at compile time, what is the best way to go about doing this?
tldr; compile debug bin print debug message, compile release bin compiler ignores debug messages.
Vio
(Viorel Grafu)
July 27, 2026, 12:04pm
2
The simplest way to achieve this in Go is by using build tags with two separate files in the same package directory.
1. Create a file named debug_on.go:
The first line must be the build tag:
//go:build debug
package main
import “log”
func DebugLog(format string, v …any) { log.Printf("[DEBUG] "+format, v…) }
2. Create a file named debug_off.go:
The first line must be:
//go:build !debug
package main
func DebugLog(format string, v …any) {}
3. Usage in code:
Just call DebugLog(“your message: %s”, value) in your main code.
4. How to compile or run:
For Debug mode (includes debug messages):
go run -tags debug .
go build -tags debug -o app-debug
For Release mode (debug function becomes an empty no-op and gets optimized away):
go run .
go build -o app-release