Golang – 离线导入/模块初始化

huangapple go评论73阅读模式
英文:

Golang - offline import/mod init

问题

相对于Go还比较新,想知道是否有办法在没有连接到互联网的情况下,使用之前在其他项目中已经调用过go getgo 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.

huangapple
  • 本文由 发表于 2021年7月1日 11:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/68204124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定