Last active
August 29, 2015 13:56
-
-
Save hayajo/9043660 to your computer and use it in GitHub Desktop.
golangでデバッグ出力
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
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") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
flagパッケージも組み合わせてみた