4 ms·
I dunno either, because it's incredibly trivial. func assert(cond bool){ if !cond { panic("whoopsies") } }
by fwip 8y ago
I dunno either, because it's incredibly trivial.
func assert(cond bool){
if !cond {
panic("whoopsies")
}
}
- rabidrat 8y agoAnd then you can just #define assert(...) to eliminate it...oh wait, Go doesn't have a macro-preprocessor.
- pstuart 8y agoTo play along.... Go does have build tags, so one could have 2 files for the assert function, each with opposite build tag logic. It does allow for an effective NOP (it would be a stub function and I believe the compiler would eliminate that).
- helper 8y agoI tested this and it does indeed eliminate that code.
- deathanatos 8y agoI'm wondering if it inlines it (and thus eliminates it). But this would come with a performance penalty: it makes the function you assert() in no longer a leaf function (it calls assert()) and AFAIK, Go only inlines leaf functions. (So, any function in which you call assert() is no longer eligible for inlining.)
- helper 8y agochrchang523 is correct. In my test the caller function can still get inlined.
- chrchang523 8y agodeathanatos: Go 1.12 can inline non-leaf functions, and I’d be surprised if it still fails to do the right thing here.
- int_19h 8y agoIt would still evaluate the condition in both cases, unless it can guarantee that the condition is a pure function with no observable side effects.