{"id":16767486,"url":"https://github.com/mfridman/buildversion","last_synced_at":"2025-11-02T03:30:28.095Z","repository":{"id":233159697,"uuid":"786181832","full_name":"mfridman/buildversion","owner":"mfridman","description":"Generate release version for Go apps","archived":true,"fork":false,"pushed_at":"2025-02-12T02:19:37.000Z","size":17,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-14T14:53:54.182Z","etag":null,"topics":["build","cli","golang","versioning"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mfridman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-13T17:04:19.000Z","updated_at":"2025-02-12T02:20:55.000Z","dependencies_parsed_at":"2024-04-14T07:59:21.041Z","dependency_job_id":"18f4341e-175f-4a55-ba75-1734cf5d52e5","html_url":"https://github.com/mfridman/buildversion","commit_stats":null,"previous_names":["mfridman/buildversion"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfridman%2Fbuildversion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfridman%2Fbuildversion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfridman%2Fbuildversion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfridman%2Fbuildversion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mfridman","download_url":"https://codeload.github.com/mfridman/buildversion/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239370440,"owners_count":19627432,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["build","cli","golang","versioning"],"created_at":"2024-10-13T06:09:14.842Z","updated_at":"2025-11-02T03:30:28.044Z","avatar_url":"https://github.com/mfridman.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# buildversion\n\n\u003e [!WARNING]\n\u003e This package is no longer required as of Go 1.24. The `runtime/debug` package now provides the\n\u003e `BuildInfo` struct, which can be used to extract the version information. For more information,\n\u003e see https://github.com/golang/go/issues/50603 or the [Go 1.24 release notes](https://go.dev/doc/go1.24#go-command).\n\u003e\n\u003e _The go build command now sets the main module’s version in the compiled binary based on the\n\u003e version control system tag and/or commit. A +dirty suffix will be appended if there are\n\u003e uncommitted changes. Use the `-buildvcs=false` flag to omit version control information from the\n\u003e binary._\n\nA simple package to generate a release version for Go applications. Compatible with Go modules.\n\nIdeal in CLI tools when you want to display the version using commands such as `mytool --version`.\n\n## Usage\n\nHere's a simple example of how you can use this package in your application. Now when `go build` or\n`go install` is run, the version will be stamped into the binary in a consistent way.\n\n```go\npackage main\n\nimport (\n\t\"flag\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/mfridman/buildversion\"\n)\n\nfunc main() {\n\tversionPtr := flag.Bool(\"version\", false, \"\")\n\tflag.Parse()\n\tif *versionPtr {\n\t\tfmt.Fprintln(os.Stdout, buildversion.New())\n\t\treturn\n\t}\n}\n```\n\n### Linking the version at build time\n\nThe reason `New()` takes a variadic `string` argument is to allow you to pass in the version at\nbuild time. This can be useful when building a release binary with tools like\n[goreleaser](https://goreleaser.com/).\n\nDefine a version variable in your main package:\n\n```go\npackage main\n\nvar version string\n\nfunc main() {\n\tbuildversion.New(version)\n}\n```\n\nThen, when building the binary, pass in the version using the `-ldflags` flag:\n\n```\ngo build -ldflags \"-X main.version=v1.2.3\" -o bin/example ./cmd/example\n```\n\n## Example\n\n### No tags (pseudo-version)\n\n```\n$ go install github.com/mfridman/buildversion/cmd/example@latest\n\nexample --version\nv0.0.0-20240413170022-fe4dc7cb6b9d\n```\n\n### Tagged release\n\n```\n$ go install github.com/mfridman/buildversion/cmd/example@latest\n\nexample --version\nv0.1.0\n```\n\n### Building from source\n\n```\n$ go build -o bin/example ./cmd/example\n\n./bin/example --version\ndevel (fe4dc7cb6b9d, dirty)\n```\n\n## But why?\n\nI've ended up copying this simple function across a few projects, so I decided to make it a package.\n\nThe `New()` function returns the version string from the\n[BuildInfo](https://pkg.go.dev/runtime/debug#BuildInfo), if available.\n\n**`New()` will always return a non-empty string.**\n\n- If the build info is not available, it returns `devel`. This can happen if the binary was built\n  without module support, if the Go version is too old or `-buildvcs=false` was set.\n\n- If building from source, it returns `devel` followed by the first 12 characters of the VCS\n  revision, followed by `, dirty` if the working directory was dirty. For example,\n\n  - `devel (abcdef012345, dirty)`\n  - `devel (abcdef012345)`\n  - `devel (unknown revision)`\n\nNote, VCS info not stamped when built listing .go files directly. For example,\n\n```\ngo build main.go\ngo build .\n```\n\nFor more information, see https://github.com/golang/go/issues/51279\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmfridman%2Fbuildversion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmfridman%2Fbuildversion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmfridman%2Fbuildversion/lists"}