Home > Blockchain >  Does there exists something like Macros in Golang for debugging
Does there exists something like Macros in Golang for debugging

Time:03-10

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)
    
    // ...
}

CodePudding user response:

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.

  •  Tags:  
  • go
  • Related