Golang testify所有包无法完整下载问题求助
解决Go mod vendor仅下载testify/assert包的问题
问题原因
Go的vendor机制是按需打包依赖:只有你的项目代码中实际导入并使用的子包,才会被纳入vendor目录。即使你执行了go get github.com/stretchr/testify拉取整个模块,若代码里没用到suite、mock等子包,go mod vendor只会打包被引用的assert包。
解决步骤
- 在项目代码中导入需要的testify子包,示例:
import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/suite" ) - 执行
go mod tidy,让Go模块更新依赖引用关系:go mod tidy - 重新运行
go mod vendor,此时所有被引用的子包都会被下载到vendor目录:go mod vendor
特殊需求:强制打包整个testify模块(不推荐)
如果不需要在代码中引用,但想强制把整个testify模块塞进vendor,可以执行:
go mod vendor -e github.com/stretchr/testify/...
注:这种做法会引入大量未使用的依赖,增加项目体积,仅在特殊场景下使用。
内容的提问来源于stack exchange,提问作者Exploring
相关产品推荐
相关产品推荐

