Skip to content

Instantly share code, notes, and snippets.

@creachadair
Created July 25, 2026 20:56
Show Gist options
  • Select an option

  • Save creachadair/5979e9b5fb12475d6e7bfeddeb3866df to your computer and use it in GitHub Desktop.

Select an option

Save creachadair/5979e9b5fb12475d6e7bfeddeb3866df to your computer and use it in GitHub Desktop.
A shell function to update an installed Go binary
#!/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