Using go.mod versions to go install a binary

Featured image for sharing metadata for article

In my post Managing your Go tool versions with go.mod and a tools.go I talked about how to track tool dependencies in a tools.go to more easily run your tools without a make setup step or similar.

However in the performance section I noted that there can be a slight performance hit with this, as go run doesn't get cached, as of Go 1.20.

Instead of go run, we can instead install the binaries, and execute them as part of go generate or our Makefile, or you may just want the binary installed to your $PATH.

To get the binary installed via our go.mod, we can run:

# make sure it's at the root of the repo
env GOBIN=$(git rev-parse --show-toplevel)/bin\
  go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen

This should be able to work out the current version of the package from your go.mod and install it appropriately πŸ™Œ

There are some cases where you may need to specify -mod manually:

# for non-vendored
env GOBIN=$(git rev-parse --show-toplevel)/bin\
  go install -mod=readonly github.com/deepmap/oapi-codegen/cmd/oapi-codegen
# or for vendoring
env GOBIN=$(git rev-parse --show-toplevel)/bin\
  go install -mod=vendor github.com/deepmap/oapi-codegen/cmd/oapi-codegen

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #go.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.