Skip to content

Instantly share code, notes, and snippets.

@shanna
Last active December 12, 2015 05:38
Show Gist options
  • Save shanna/4723245 to your computer and use it in GitHub Desktop.
Save shanna/4723245 to your computer and use it in GitHub Desktop.
Evil or awesome use of tags in golang for debugging?
#!/bin/sh
go test -test.bench . 2> /dev/null
go test -tags debug -test.bench . 2> /dev/null
PASS
BenchmarkDebug 100000000 13.3 ns/op
ok _/home/shane/projects/log 1.356s
PASS
BenchmarkDebug 2000000 839 ns/op
ok _/home/shane/projects/log 2.541s
// +build debug
package log
import (
"fmt"
"os"
)
// Complicated as you like, include line numbers and such.
func Debugf(format string, v ...interface{}) {
fmt.Fprintf(os.Stderr, format, v...)
}
// +build !debug
package log
// Just a stub.
func Debugf(format string, v ...interface{}) {}
package log
import (
"testing"
)
func BenchmarkDebug(b *testing.B) {
for i := 0; i < b.N; i++ {
Debugf("test")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment