{"id":17877823,"url":"https://github.com/ropes/go-linker-vars-example","last_synced_at":"2025-09-10T18:48:39.303Z","repository":{"id":57545267,"uuid":"48518531","full_name":"Ropes/go-linker-vars-example","owner":"Ropes","description":"This project is a simple example of using Go build linker flags to set package variables deeper than main","archived":false,"fork":false,"pushed_at":"2015-12-28T09:03:02.000Z","size":4,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T08:48:19.745Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ropes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-24T01:28:28.000Z","updated_at":"2019-03-30T07:40:06.000Z","dependencies_parsed_at":"2022-09-16T23:40:46.445Z","dependency_job_id":null,"html_url":"https://github.com/Ropes/go-linker-vars-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ropes%2Fgo-linker-vars-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ropes%2Fgo-linker-vars-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ropes%2Fgo-linker-vars-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ropes%2Fgo-linker-vars-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ropes","download_url":"https://codeload.github.com/Ropes/go-linker-vars-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244912800,"owners_count":20530764,"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":[],"created_at":"2024-10-28T11:59:08.023Z","updated_at":"2025-03-22T05:31:29.409Z","avatar_url":"https://github.com/Ropes.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-linker-vars-example\nThis project is a simple example of using `go build` linker flags to set package variables deeper than just main which is well covered. I created this repo because I didn't find a good example on the topic of deeper variables which could be imported into multiple main packages. Full documentation for the [go command](https://golang.org/cmd/go/).\n\n**NOTE: This repo example repo is built using Go 1.5.2, I know for a fact the flags changed since 1.4.x. However the same concepts should are available just check the documentation**\n\n## Simple Configuration of `main` package variables\n```\ncd cmd/\ngo build -ldflags \"-X main.MainVar=hihi\"\n\n$ ./cmd\nMainVar: hihi\nVersion: \n```\n\n## Setting pathed package variables with linker flags\nThe `github.com/ropes/go-linker-vars-example/src/version` can then be imported into any Go package to access its exported variables.\n```\ncd cmd/\ngo build -ldflags \"-X github.com/ropes/go-linker-vars-example/src/version.Version=hihi\"\n./cmd\n\nOutput:\n\nMainVar: \nVersion: hihi\n```\n\n# Getting fancy with it!\nImport the `version` package to where it will be exposed or logged:\n```go\npackage main\n\nimport \"fmt\"\nimport \"github.com/ropes/go-linker-vars-example/src/version\"\n\nfunc main() {\n\tfmt.Printf(\"Git Tag:   %s\\n\", version.GitTag)\n\tfmt.Printf(\"Build User: %s\\n\", version.BuildUser)\n\tfmt.Printf(\"Version:   %s\\n\", version.Version)\n}\n```\n\nInject VCS and build information into the an importable package which can be referenced by multiple binaries via the `go ``build` or `install` commands. \n```\ncd cmd/complex\ngittag=$(git describe --tags)\n\ngo build -ldflags \"-X github.com/ropes/go-linker-vars-example/src/version.GitTag=${gittag} \n-X github.com/ropes/go-linker-vars-example/src/version.BuildUser=${USER} \n-X github.com/ropes/go-linker-vars-example/src/version.Version=v0.0.1\"\n\nOutput:\n$./complex \nGit Tag:    v0.0.1-1-gd75e8db\nBuild User: josh\nVersion:    v0.0.1\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fropes%2Fgo-linker-vars-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fropes%2Fgo-linker-vars-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fropes%2Fgo-linker-vars-example/lists"}