Go 1.18 added a feature to easily get the Git commit version that the binary was built from.

This is so much easier than the old way of doing it!

package main

import (
    "fmt"
    "runtime/debug"
)

func main() {
    info, _ := debug.ReadBuildInfo()

    for _, setting := range info.Settings {
        if setting.Key == "vcs.revision" || setting.Key == "vcs.time" {
            fmt.Printf("%s:\t%s\n", setting.Key, setting.Value)
        }
    }
}
$ go build git-version
$ ./git-version
vcs.revision:   4d47dd39d1debbdf10715fd9ff967e09d4347f02
vcs.time:   2023-09-02T00:21:21Z

🎉

More info here.