debug.go (1275B)
1 package debug 2 3 import ( 4 _debug "runtime/debug" 5 ) 6 7 // Run will only call fn if DEBUG is enabled. 8 func Run(fn func()) { 9 if DEBUG { 10 fn() 11 } 12 } 13 14 // BuildInfo will return a useful new-line separated build info string for current binary, setting name as given value. 15 func BuildInfo(name string) string { 16 // Read build info from current binary 17 build, ok := _debug.ReadBuildInfo() 18 if !ok { 19 return "name=" + name + "\n" 20 } 21 22 var flags, vcs, commit, time string 23 24 // Parse build information from BuildInfo.Settings 25 for i := 0; i < len(build.Settings); i++ { 26 switch build.Settings[i].Key { 27 case "-gcflags": 28 flags += ` -gcflags="` + build.Settings[i].Value + `"` 29 case "-ldflags": 30 flags += ` -ldflags="` + build.Settings[i].Value + `"` 31 case "-tags": 32 flags += ` -tags="` + build.Settings[i].Value + `"` 33 case "vcs": 34 vcs = build.Settings[i].Value 35 case "vcs.revision": 36 commit = build.Settings[i].Value 37 if len(commit) > 8 { 38 commit = commit[:8] 39 } 40 case "vcs.time": 41 time = build.Settings[i].Value 42 } 43 } 44 45 return "" + 46 "name=" + name + "\n" + 47 "vcs=" + vcs + "\n" + 48 "commit=" + commit + "\n" + 49 "version=" + build.Main.Version + "\n" + 50 "path=" + build.Path + "\n" + 51 "build=" + build.GoVersion + flags + "\n" + 52 "time=" + time + "\n" 53 }