在golang模块功能中本地安装软件包 [英] Install packages locally in golang module feature
问题描述
当我使用go get安装软件包时,在golang模块模式下,这些软件包将安装在go根文件夹中.
When I install a package using go get, in the golang module mode, these are installed in the go root folder.
我希望能够以与npm或yarn对node.js(全局或本地)相同的方式安装模块.
I would like to be able to install the modules in the same way that npm or yarn does for node.js, global or local.
什么是实现此行为的正确方法.
what would be the right way to achieve this behavior.
谢谢.
推荐答案
正如其他人所提到的,您可以像这样创建模块文件:
As others have mentioned, you can create your module files like this:
go mod init [import path]
之后,您可以随时运行此命令以将所有依赖项移至 vendor
目录:
After that you can run this command any time to move all dependencies into the vendor
directory:
go mod vendor
这与 node_modules
目录的行为非常相似.要使用此目录中的依赖项进行构建,可以将 -mod供应商
添加到构建命令中:
This is very similar to the behaviour of the node_modules
directory.
To build using the dependencies in this directory, you can add -mod vendor
to your build command:
go build -mod vendor
通过这种方式,您可以将项目复制到任何计算机上,而无需在其他计算机上获取任何内容.
This way you can copy your project to any machine without go-getting anything on that other machine.
根据文档, -mod = vendor
如果 go.mod
文件中的Go版本为 1.14
或更高版本并且存在 vendor
目录,则会自动设置标记.
According to the docs, the -mod=vendor
flag is set automatically if the Go version in the go.mod
file is 1.14
or higher and a vendor
directory exists.
要从 vendor
目录中清除未使用的依赖项,可以运行
To clean unused dependencies from the vendor
directory, you can run
go mod tidy
这篇关于在golang模块功能中本地安装软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!