Home >Backend Development >Golang >How Can I Install Go Tools Using Go Modules Without Breaking My Project?
go modules: Installing Go Tools
When using go modules as your dependency management system, you may encounter difficulties installing tools using the syntax:
go get -u github.com/go-critic/go-critic/...
This can result in an error:
go: cannot find main module; see 'go help modules'
To resolve this, consider these two scenarios:
Case 1: Installing Tools Without Modifying Your go.mod
If you do not intend to track the tool as a dependency in your go.mod, you have several options:
For more details, see this related answer, which includes solutions for Go 1.11 and an upcoming option in Go 1.14.
Case 2: Tracking Tool Dependencies
If you wish to explicitly track a tool as a dependency in your go.mod, follow these steps:
//go:build tools // +build tools package tools import ( _ "golang.org/x/tools/cmd/stringer" )
The import statements will record tool version information in your go.mod, while the // build tools constraint prevents normal builds from importing your tools.
The above is the detailed content of How Can I Install Go Tools Using Go Modules Without Breaking My Project?. For more information, please follow other related articles on the PHP Chinese website!