#compile without fmt.Println() for better performace
23 messages · Page 1 of 1 (latest)
is print() any faster?
Have a const debug that you make false before compiling, then everything that says
if debug {
fmt.Println("")
}```will get removed.
does the stop the import?
There's nothing to import if all its uses are eliminated
but that looks like a run time checking which requires it to be there in case the debug value changes to true ?
but debug is a const
so the theory is that since debug is a const, and it's false, it will never be true
therefore the compiler could simply omit that branch from the final output
with that said, it's theory, i have no idea on how i would prove it
compiler output if const is true https://go.godbolt.org/z/ozdbh6xGo
if const is false https://go.godbolt.org/z/s8z6x6oYT
i dont have enough knowledge to say if that's correct, but the one where const is false, seems to result in smaller output
@candid oxide is correct, if your debug flag is a const, it can never change at runtime, so the check will be fully removed at compile time.
sadly you cant use go variable injection to overwrite a const
only strings can be overwritten
@candid oxide just use tags
yep was about to ask that
can you set build tags specific to a package
or do you just namespace the tag itself
since just using "debug" build tags might turn on debug for everyone's packages
Thank you @boreal void @candid oxide @tame ore
But the only thing that is bothering me is that if we have multiple packages in a project, we need to set the Debug const to false for each of them before compiling.
Can't set them all using ldflags since the ldflag can only add to a Var.
just use tags:
just use tags