在 Go 1.24+ 中如何管理工具依赖项 - Alex Edwards

Go 1.24 introduces new functionality for managing developer tooling dependencies. Tooling includes static code analysis tools like staticcheck, vulnerability scanning tools like govulncheck, and live-reloading tools like air. Historically, managing these dependencies was tricky using tools.go file or go run pattern. With Go 1.24, there's a better way.

Key Commands and Features

  • go get -tool import_path@version to add tools to module.
  • go tool command to run tools from command line, in makefiles, or with go:generate.
  • go list tool to list added tools.
  • go mod verify to check tool code in module cache.
  • go mod vendor to include tooling code in vendor folder.
  • go get -tool import_path@version to upgrade or downgrade tools.
  • go get -tool @none to remove tools.
  • Use a separate modfile for tool dependencies with -modfile flag.

Example

  • Scaffold a simple module and add application code.
  • Use go get -tool to add tools like stringer, govulncheck, and staticcheck.
  • Run tools using different methods like from command line, makefile, or go:generate.
  • See how vendoring tools works and its limitations.

Risks and Considerations

  • If tools share dependencies with application code, it may cause version conflicts and break application code.
  • Using a separate modfile for tools can reduce this risk.
阅读 5
0 条评论