Go Tools as Deps
Back2023-04-17
Scenario: I want to run golangci-lint
occasionally or in CI to make sure I'm not doing bad things. BUT since it's not in my code
I can't add it to the go.mod
.
Solution!!!!
Create a file called tools.go
with the following content:
//go:build tools
// +build tools
package tools
import (
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
)
Now we can run
go get github.com/golangci/golangci-lint/cmd/golangci-lint
and it'll stay in the mod
file since it's in the code, but will never actually be built.
And now when we test it out:
go run github.com/golangci/golangci-lint/cmd/golangci-lint run --color always -v -c ./.golangci.yml
INFO [config_reader] Used config file .golangci.yml
INFO [lintersdb] Active 9 linters: [errcheck gofmt gosimple govet ineffassign nestif staticcheck typecheck unused]
...
...