Created
July 25, 2026 20:56
-
-
Save creachadair/5979e9b5fb12475d6e7bfeddeb3866df to your computer and use it in GitHub Desktop.
A shell function to update an installed Go binary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # This takes advantage of the fact that "go version -m" writes out | |
| # build metadata from a Go binary. We use this to extract the import | |
| # path of the program, as well as any build tags it was built with. | |
| # The latter we want so that when we update, we will get the same | |
| # build tags. | |
| # | |
| # Note, however, that this will not work for cross-compiled programs. | |
| # You could also extract GOOS and GOARCH and set those, but this script | |
| # doesn't include that. | |
| inst() { | |
| local t="${1:?missing file path}" | |
| shift 1 | |
| local ipath tags | |
| ipath="$(go version -m --json "$t" | jq -r .Path)" | |
| tags="$(go version -m --json "$t" | jq -r '.Settings[]|select(.Key == "-tags").Value')" | |
| echo "P $ipath" ${@+"$@"} 1>&2 | |
| go install ${tags:+-tags "$tags"} "$@" "$ipath"@latest | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment