Skip to content

Instantly share code, notes, and snippets.

@hayajo
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayajo/9043660 to your computer and use it in GitHub Desktop.
Save hayajo/9043660 to your computer and use it in GitHub Desktop.
golangでデバッグ出力
package main
import (
"flag"
"log"
"os"
"runtime"
)
var debug debugT = false
type debugT bool
func (d debugT) Printf(format string, args ...interface{}) {
if d {
pc, file, line, _ := runtime.Caller(1)
args = append([]interface{}{pc, file, line}, args...)
log.Printf("%d %s %d: " + format, args...)
}
}
func init() {
if os.Getenv("GODEBUG") != "" {
debug = true
} else {
usage := "debug"
ptr := (*bool)(&debug)
flag.BoolVar(ptr, "debug", false, usage)
flag.BoolVar(ptr, "d", false, usage + " (shorthand)")
}
}
func main() {
flag.Parse()
debug.Printf("this is %s message", "DEBUG")
}
@hayajo
Copy link
Author

hayajo commented Feb 18, 2014

flagパッケージも組み合わせてみた

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