英文:
Golang - offline import/mod init
问题
相对于Go还比较新,想知道是否有办法在没有连接到互联网的情况下,使用之前在其他项目中已经调用过go get
或go mod tidy
的依赖项来启动一个新项目?是否有办法导入整个包,而无需重新连接到互联网来下载/整理同一依赖项的其他子包?
我之所以问这个问题,是因为我通常在编码时没有互联网访问,所以要启动项目变得非常麻烦。
到目前为止,似乎唯一有效的方法是复制旧项目并清空它,但这似乎有点荒谬,即使不必从mod/sum文件中明确删除不想再次使用的内容。我肯定是漏掉了什么。
我的$GOPATH指向~/go
我希望使用的包在$GOPATH/pkg/mod中。将pkg/mod放在$GOPATH上是否太过分了?
感谢您能提供的任何帮助!
英文:
Relatively new to Go and wondering if there's any way to start a new project using a dependency you've already called go get
or go mod tidy
for in a previous project, without being connected to the internet?
Is there any way to import a whole package without having to reconnect to the internet to download/tidy further subpackages from the same dependency?
Reason I ask is that I don't normally have internet access where I code, so it becomes quite annoying to faff about to get things off the ground.
So far the only thing that seems to work is copying the old project and clearing it out, but that seems kind of ridiculous, even without having to specifically remove things you don't want to use again from the mod/sum files. Surely I'm missing something?
My $GOPATH points to ~/go
The package(s) I hope to use are in $GOPATH/pkg/mod. Would it be too egregious to place pkg/mod on the $GOPATH?
Thanks for any help you can give!
答案1
得分: 2
你可以使用vendoring。运行以下命令:
go mod vendor
这将创建一个"vendor"目录,go工具将使用其中的依赖项。
更多信息请参阅https://golang.org/ref/mod#go-mod-vendor。
英文:
You can use vendoring. Run
go mod vendor
This will create a "vendor" directory and the go tool will use the dependencies from there.
See https://golang.org/ref/mod#go-mod-vendor fore more information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论