go install でツールのバイナリをインストールできましたが、Go 1.15 以前ではインストール時にバージョン指定ができませんでした。インストール時に go.mod にバージョンが追記されますが、どこからも import されていない場合 go mod tidy で依存関係が削除されてしまいます。そこで Build Constraints を利用するテクニックが知られています。ブランクインポートにより import されているが、Build Constraints でコンパイル/ビルドはされないようにしています。Build Constraints を利用する Go のファイル例の記載例は以下のようなものです。ファイル名は慣習として tools.go とすることが多いでしょう。
tools.go
// +build tools
package tools
import ( _ "golang.org/x/tools/cmd/stringer" )
go install でバージョンを指定する方法
Go 1.16 からは go install でインストール時にバージョン指定できるようになりました。このリリースにより go.mod の依存関係に影響を与えず、ツールをインストールできるようになりました。このあたりの話は、過去のGo1.16のリリース連携記事(Go 1.16のgo installについて)でも触れています。
余談ですが、私たちのチームでは go install でバージョン指定してインストールするコマンドを Makefile などにまとめて記載することがよくあります。
Makefile
.PHONY: install
install: go install golang.org/x/tools/cmd/stringer@v0.28.0 go install github.com/sqlc-dev/sqlc/cmd/sqlc@v1.27.0
Available Commands: compile Statically check SQL for syntax and type errors completion Generate the autocompletion script for the specified shell createdb Create an ephemeral database diff Compare the generated files to the existing files generate Generate source code from SQL help Help about any command init Create an empty sqlc.yaml settings file push Push the schema, queries, and configuration for this project verify Verify schema, queries, and configuration for this project version Print the sqlc version number vet Vet examines queries