-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoversion.go
More file actions
41 lines (33 loc) · 836 Bytes
/
Copy pathgoversion.go
File metadata and controls
41 lines (33 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"fmt"
"runtime/debug"
"strings"
)
const progname = "goversion"
var version string
func main() {
fmt.Printf("version from make: %s %s\n", progname, version)
fmt.Printf("buildinfo: %s %s\n", progname, readBuildInfo())
}
func readBuildInfo() string {
if info, ok := debug.ReadBuildInfo(); ok {
// When built with go install ...@version
version := info.Main.Version
if version != "(devel)" {
fmt.Printf("got it from info.Main.Version\n")
return version
}
// Otherwise, use Git commit data.
fmt.Printf("got it from Git\n")
sb := strings.Builder{}
sb.WriteString("devel")
for _, setting := range info.Settings {
if strings.HasPrefix(setting.Key, "vcs") {
sb.WriteString(fmt.Sprintf(" %s=%s", setting.Key, setting.Value))
}
}
version = sb.String()
}
return version
}