Skip to content

Instantly share code, notes, and snippets.

@Bwooce
Last active January 11, 2016 18:01
Show Gist options
  • Save Bwooce/5381866 to your computer and use it in GitHub Desktop.
Save Bwooce/5381866 to your computer and use it in GitHub Desktop.
Go debug code from Rob Pike. Updated with a Println that works (thanks guelfey from #go-nuts)
const debug debugging = true // or flip to false
type debugging bool
func (d debugging) Println(args ...interface{}) {
if d {
log.Println(append([]interface{}{"DEBUG:"}, args...)...)
}
}
func (d debugging) Printf(format string, args ...interface{}) {
if d {
log.Printf("DEBUG:" + format, args...)
}
}
@pciet
Copy link

pciet commented Jan 11, 2016

My understanding is that this is from https://github.com/golang/glog and relies on the compiler inlining these nop functions when the const debug is set to false (https://groups.google.com/forum/#!topic/golang-nuts/TzuGe1rcQ4Y).

I'm personally using this pattern only as a temporary solution for in-line debugging calls in application code, as the compiler output still has effects from the debug code (nop calls and code memory use). This 'if debug' pattern relies on a compilation feature (inlining functions/methods with no effects, and removing the nop inline) that has to be implemented correctly for each platform to work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment