https://github.com/fortio/version
Reusable wrappers/utils to extract version(s) from golang Buildinfo included in `go install`ed binaries
https://github.com/fortio/version
Last synced: about 2 months ago
JSON representation
Reusable wrappers/utils to extract version(s) from golang Buildinfo included in `go install`ed binaries
- Host: GitHub
- URL: https://github.com/fortio/version
- Owner: fortio
- License: apache-2.0
- Created: 2023-02-12T20:41:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-23T16:29:51.000Z (6 months ago)
- Last Synced: 2025-06-17T08:08:33.496Z (4 months ago)
- Language: Go
- Size: 19.5 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# version
Reusable wrappers/utils to extract version(s) from golang Buildinfo included in `go install`ed binaries.Note that as of go 1.24, `go build` also embeds the same information if your git repo is clean (add the necessary entries in your .gitignore to make it so).
```golang
import fortio.org/versionvar shortVersion, longVersion, fullVersion := version.FromBuildInfo()
```Complete example in [sample/simpleMain.go](sample/simpleMain.go)
See also a tiny library using this one to reduce the repetition in main programs: [fortio.org/cli](https://github.com/fortio/cli#cli) package.
Yielding:
```shell
$ go install fortio.org/version/sample@latest
go: downloading fortio.org/version v1.0.0
$ ~/go/bin/sample -h
fortio.org/version sample main 1.0.0 usage:
/Users/dl/go/bin/sample [flags]
flags:
-buildinfo
Show full build info and exit.
$ ~/go/bin/sample -buildinfo
1.0.0 h1:JbGoGiNQ0883KoVPDsYhQCQ32QkAVTtECn86XVRRYi4= go1.19.5 arm64 darwin
go go1.19.5
path fortio.org/version/sample
mod fortio.org/version v1.0.0 h1:JbGoGiNQ0883KoVPDsYhQCQ32QkAVTtECn86XVRRYi4=
build -compiler=gc
build CGO_ENABLED=1
build CGO_CFLAGS=
build CGO_CPPFLAGS=
build CGO_CXXFLAGS=
build CGO_LDFLAGS=
build GOARCH=arm64
build GOOS=darwin
$ ~/go/bin/sample
2023/02/12 13:34:37 fortio.org/version sample main started 1.0.0 h1:JbGoGiNQ0883KoVPDsYhQCQ32QkAVTtECn86XVRRYi4= go1.19.5 arm64 darwin
```Libraries can embed their own version using for instance
```golang
func init() { //nolint:gochecknoinits // we do need an init for this
shortVersion, longVersion, fullVersion = version.FromBuildInfoPath("fortio.org/fortio")
}
```and the `longVersion` in this example can then show both the module/library version and the version of the containing binary. eg
fortio 1.40.0 h1:jSDO/jGcyC/qTpMZZ84EZbn9BQawsWM9/RMQ9s6Cn3w= go1.19.5 arm64 darwin (in fortio.org/fortiotel 1.3.0)