Skip to content

Instantly share code, notes, and snippets.

@tomill
Created March 16, 2022 08:42
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 tomill/620ab7d00647a06b1091966dc9032d6f to your computer and use it in GitHub Desktop.
Save tomill/620ab7d00647a06b1091966dc9032d6f to your computer and use it in GitHub Desktop.
go 1.18 debug.ReadBuildInfo() testing
package main
import (
"flag"
"fmt"
"runtime/debug"
"strings"
)
var (
version = "(development)"
showVersion = flag.Bool("version", false, "show version")
)
// instead of go build --ldflags '-X "version=`git rev-parse HEAD`"'
func init() {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for _, s := range info.Settings {
if strings.Contains(s.Key, "vcs.revision") {
version = s.Value
break
}
}
}
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("version: build %s\n", version)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment