Skip to content

Instantly share code, notes, and snippets.

@Bhupesh-V
Created February 15, 2024 07:33
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 Bhupesh-V/9574109e6aaebe65cb054e8b3997867a to your computer and use it in GitHub Desktop.
Save Bhupesh-V/9574109e6aaebe65cb054e8b3997867a to your computer and use it in GitHub Desktop.
Providing Git VCS commit info from Go
package main
import (
"fmt"
"log"
"runtime/debug"
)
func main() {
// Only works when we do a main module build (doesn't work with direct go run)
// See https://github.com/golang/go/issues/51279
info, _ := debug.ReadBuildInfo()
bi, err := debug.ParseBuildInfo(info.String())
if err != nil {
log.Fatal(err)
}
for _, dep := range bi.Settings {
if dep.Key == "vcs.revision" || dep.Key == "vcs.time" {
fmt.Println(dep.Key, dep.Value)
}
}
}
vcs.revision ab83d948829673d24206a1e0bc67brf400cdb059
vcs.time 2024-02-15T07:26:01Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment