Last active
January 11, 2016 18:01
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.