英文:
Golang equivalent of npm install -g
问题
如果我有一个已编译的 Golang 程序,我想要安装它,以便我可以在计算机的任何位置使用 bash 命令运行它,我该如何做呢?例如,在 Node.js 中,我们可以使用以下命令安装 express:
npm install -g express
这样,我就可以运行以下命令:
express myapp
express 将在当前目录下生成一个名为 "myapp" 的文件目录,用于创建一个 Node 应用程序。在 Go 中是否有类似的命令呢?我相信现在使用 "go install" 命令时,必须在包含可执行文件的目录中才能运行它。
提前感谢您的回答!
英文:
If I had a compiled Golang program that I wanted to install such that I could run it with a bash command from anywhere on my computer, how would I do that? For example, in nodejs
npm install -g express
Installs express such that I can run the command
express myapp
and express will generate a file directory for a node application called "myapp" in whatever my current directory is. Is there an equivalent command for go? I believe now with the "go install" command you have to be in the directory that contains the executable in order to run it
Thanks in advance!
答案1
得分: 49
更新:如果您正在使用Go 1.16,那么这个答案仍然有效,但是go install
已经发生了变化,现在是安装可执行包的推荐方法。请参考Karim的答案进行解释:https://stackoverflow.com/a/68559728/10490740
在使用Go >= 1.11时,如果您的当前目录位于基于模块的项目中,或者您在环境中设置了GO111MODULE=on
,那么go get
将不会将包“全局”安装。它将把它们添加到您项目的go.mod文件中。
从Go 1.11.1开始,设置GO111MODULE=off
可以绕过这种行为:
GO111MODULE=off go get github.com/usr/repo
基本上,通过禁用此单个命令的模块功能,它将按预期安装到GOPATH中。
未使用模块的项目仍然可以正常使用go get
将二进制文件安装到$GOPATH/bin
。
关于这种行为变化的讨论和多个问题记录在这里:golang/go - cmd/go: go get should not add a dependency to go.mod #27643。
英文:
Update: If you're using Go 1.16, this answer still works, but go install
has changed and is now the recommended method for installing executable packages. See Karim's answer for an explanation: https://stackoverflow.com/a/68559728/10490740
Using Go >= 1.11, if your current directory is within a module-based project, or you've set GO111MODULE=on
in your environment, go get
will not install packages "globally". It will add them to your project's go.mod file instead.
As of Go 1.11.1, setting GO111MODULE=off
works to circumvent this behavior:
GO111MODULE=off go get github.com/usr/repo
Basically, by disabling the module feature for this single command, it will install to GOPATH as expected.
Projects not using modules can still go get
normally to install binaries to $GOPATH/bin
.
There's a lengthy conversation and multiple issues logged about this change in behavior branching from here: golang/go - cmd/go: go get should not add a dependency to go.mod #27643.
答案2
得分: 34
从Go >= 1.16开始,安装可执行文件的推荐方法是使用以下命令:
go install package@version
例如,go install github.com/fatih/gomodifytags@latest
。
可执行文件(主包)将安装到由GOBIN
环境变量指定的目录中,默认为$GOPATH/bin
或者如果未设置GOPATH
环境变量,则为$HOME/go/bin
。您需要将此目录添加到PATH
变量中,以便全局运行可执行文件。在我的情况下,我已将以下行添加到我的~/.zshrc
文件中(如果您使用的是bash,请将其添加到~/.bash_profile
文件中):
export PATH="$HOME/go/bin:$PATH"
Go团队在一篇博文中介绍了这个变化,以下是他们的解释:
我们过去推荐使用
go get -u program
来安装可执行文件,但这种用法在go.mod中添加或更改模块版本要求的含义上引起了太多的混淆。
请参考go install
文档了解更多详情。
英文:
Starting with Go >= 1.16 the recommended way to install an executable is to use
go install package@version
For example, go install github.com/fatih/gomodifytags@latest
.
Executables (main packages) are installed to the directory named by the GOBIN
environment variable, which defaults to $GOPATH/bin
or $HOME/go/bin
if the GOPATH
environment variable is not set. You need to add this directory to your PATH
variable to run executables globally. In my case, I've added this line to my ~/.zshrc
file. (if you are using bash, add it to the ~/.bash_profile
file):
export PATH="$HOME/go/bin:$PATH"
Go team published a blog post about this change, here's the explanation quote:
> We used to recommend go get -u program to install an executable, but this use caused too much confusion with the meaning of go get for adding or changing module version requirements in go.mod.
Refer to go install
documentation for more details
答案3
得分: 24
据我所知,npm install -g
没有直接的等价物。最接近的等价物不是 go install
,而是 go get
。根据帮助页面 (go help get
):
用法:go get [-d] [-f] [-fix] [-insecure] [-t] [-u] [build flags] [packages]
go get
下载并安装由导入路径指定的包,以及它们的依赖项。
默认情况下,go get
将二进制文件安装到 $GOPATH/bin
,所以让这些二进制文件在任何地方都可调用的最简单方法是将该目录添加到 $PATH
中。
为此,请将以下行添加到您的 .bashrc
(或 .zshrc
,取决于您使用的是哪个 shell)中:
export PATH="$PATH:$GOPATH/bin"
或者,您还可以将可执行文件复制或链接到 /usr/local/bin
:
ln -s $GOPATH/bin/some-binary /usr/local/bin/some-binary
英文:
As far as I know, there is no direct equivalent to npm install -g
. The closest equivalent would not be go install
, but go get
. From the help page (go help get
):
>usage: go get [-d] [-f] [-fix] [-insecure] [-t] [-u] [build flags] [packages]
>
>Get downloads and installs the packages named by the import paths,
along with their dependencies.
By default, go get
installs binaries to $GOPATH/bin
, so the easiest way to make those binaries callable from everywhere is to add that directory to your $PATH
.
For this, put the following line into your .bashrc
(or .zshrc
, depending on which shell you're using):
export PATH="$PATH:$GOPATH/bin"
<hr />
Alternatively, you could also copy or link the executables to /usr/local/bin
:
ln -s $GOPATH/bin/some-binary /usr/local/bin/some-binary
答案4
得分: 9
Linux用户的简短解决方案:
- 像往常一样使用
go get
命令。 - 在
.bashrc
文件中添加以下几行:
# 这是默认的GOPATH路径,你可以通过执行'go env'命令来确认
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
- 重新启动终端或者执行
source
命令。安装的二进制文件将在全局范围内可用。
对于Go v1.8+版本:
- 使用
go install package_name@latest
命令安装。
英文:
Short solution for Linux users:
- Use the
go get
command as usual - Add the following lines to
.bashrc
:
# This is the default GOPATH, you should confirm with the 'go env' command
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
- Restart terminal or source it. Installed binaries will be available globally.
For Go v1.8+
go install package_name@latest
答案5
得分: 5
警告:根据2020年废弃go get
的情况,此答案已过时。这里提供的解决方案在较新的Go运行时安装中将无法使用。
在Go中,与此最接近的类似功能是go get
命令。默认情况下,它会从提供的存储库URL中获取一个Go包,并需要在您的shell中设置$GOPATH
变量,以便Go知道在哪里存储这些包(以及在编译依赖于go get
安装的包的代码时在哪里找到它们)。
示例语法:
$ go get github.com/user/repo
npm
的-g
标志提供的行为是默认的,使用go get
安装的包通常是全局可用的。
有关该命令的更多信息,请参阅go get --help
。
如@helmbert所提到的,如果您正在安装独立包,将您的$GOPATH
添加到$PATH
中会很有用。
英文:
> Caveat: this answer is outdated following the 2020 deprecation of go get
. The solution presented here won't work with newer Go runtime installs.
The closest analogue of this in Go would be go get
. By default, it will fetch a Go package from a supplied repository URL, and requires a $GOPATH
variable to be set in your shell so that Go knows where to store the packages (and subsequently where to find them when compiling code depending on go get
-ted packages).
Example syntax:
$ go get github.com/user/repo
The behaviour supplied by npm
's -g
flag is default, and packages installed using go get
are normally available globally.
See go get --help
for more information about the command.
As mentioned by @helmbert, adding your $GOPATH
to your $PATH
is useful if you're installing standalone packages.
答案6
得分: 2
如果你正在使用zsh:
首先,使用以下命令安装你的包:
go install package@version
然后,编辑你的 .zshrc 文件:
nano ~/.zshrc
在 .zshrc 文件的末尾添加以下行:
export PATH="$HOME/go/bin:$PATH"
最后但同样重要的是:
source ~/.zshrc
然后打开一个新的终端并执行你的命令
英文:
if you are using zsh :
first: install your package using :
go install package@version
then , you edit your .zshrc file
nano ~/.zshrc
Add this line to the end of .zshrc file :
export PATH="$HOME/go/bin:$PATH"
last but not least :
source ~/.zshrc
then open a new terminal and execute your command
答案7
得分: 0
TL;DR:我将为您提供翻译的部分,不包括代码部分。以下是翻译的内容:
在看到这个问题时,我想:“如果我能将root的GOPATH=/usr
,那么它会将东西安装在/usr/bin/
和/usr/src
下!”
所以我尝试了显而易见的方法:
-
将
GOPATH=/usr
添加到root的.bashrc
文件中。
它起作用了!
有点。
实际上并没有。
事实证明,sudo
不会执行root的.bashrc
文件。出于“安全”或其他原因。 -
在
/etc/sudoers
中进行env_set
或类似的操作。
事实证明,/etc/sudoers
只能删除环境变量。没有env_set
指令。
(据我所知) -
查阅
man sudoers
。
sudo从哪里获取其默认的环境变量集?
嗯,列表中的第一个是/etc/environment
,所以我使用了它。
sudo echo "GOPATH=/usr" >> /etc/environment
sudo go get
可执行文件将被放置在/usr/bin
中,源代码将被放置在/usr/src
中。
以非root用户运行go将按照“正常”的方式使用GOPATH。
英文:
TL;DR at the bottom. I'm going to walk you through how I came to this conclusion, and why the more obvious solutions don't work.
Upon seeing this question, I thought "If I could set root's GOPATH=/usr
, it would install things in /usr/bin/
and /usr/src
!"
So I tried the obvious thing:
-
Add
GOPATH=/usr
to root's.bashrc
.
And it worked!
Sort of.
Not really.
Turns out,sudo
doesn't execute root's.bashrc
. For "security" or something like that. -
Do
env_set
or something in/etc/sudoers
Turns out,/etc/sudoers
can only remove environment variables. There's noenv_set
directive.
<sup>(As far as I can find)</sup> -
Dig through
man sudoers
.
Where does sudo get it's default set of environment variables from?
Well, the first one in the list is/etc/environment
, so that's the one I used.
sudo echo "GOPATH=/usr" >> /etc/environment
sudo go get <repo>
Binaries will be put in /usr/bin
, and sources will be put in /usr/src
.
Running go as non-root will use GOPATH the "normal" way.
答案8
得分: 0
如果您没有安装Go,可以使用gobinaries。它会从GitHub仓库构建项目的即时二进制文件。
安装Go包的命令如下:
curl -sf https://gobinaries.com/rakyll/hey | sh
英文:
If you don't have go installed, you may use the gobinaries. it builds an on-demand binary of the project from github repo.
The command to install the go package would be:
curl -sf https://gobinaries.com/rakyll/hey | sh
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论