Does there exists something like Macros in Golang for debugging

Issue

Does there exist also something like macros of C/C++ in golang so that in debug phase, developers are allowed to print some additional debug information? And in the official release, just set these macros to false so that they will not be compiled and no extra information will be printed. Here is a snippet to illustrate what I mean.

func demo() {
    // ...
    // the following line will not be compiled in release only in debug phase
    
    printMyDebugInfo(variable)
    
    // ...
}

Solution

The closest is probably to define a debug print function in two versions.

One for debug mode:

//go:build debug

package whatever

func debugPrint(in string) {
    print(in)
}

One for production:

//go:build !debug

package whatever

func debugPrint(in string) {}

Then use go build -tags debug when you want to use the debug version.

Answered By – Adrian

Answer Checked By – Terry (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.