Golang中相当于npm install -g的命令是什么?

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

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用户的简短解决方案:

  1. 像往常一样使用go get命令。
  2. .bashrc文件中添加以下几行:
# 这是默认的GOPATH路径,你可以通过执行&#39;go env&#39;命令来确认
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
  1. 重新启动终端或者执行source命令。安装的二进制文件将在全局范围内可用。

对于Go v1.8+版本:

  1. 使用go install package_name@latest命令安装。
英文:

Short solution for Linux users:

  1. Use the go get command as usual
  2. Add the following lines to .bashrc:
# This is the default GOPATH, you should confirm with the &#39;go env&#39; command
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
  1. Restart terminal or source it. Installed binaries will be available globally.

For Go v1.8+

  1. 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

然后打开一个新的终端并执行你的命令 Golang中相当于npm install -g的命令是什么?

英文:

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=&quot;$HOME/go/bin:$PATH&quot;

last but not least :

source ~/.zshrc

then open a new terminal and execute your command Golang中相当于npm install -g的命令是什么?

答案7

得分: 0

TL;DR:我将为您提供翻译的部分,不包括代码部分。以下是翻译的内容:

在看到这个问题时,我想:“如果我能将root的GOPATH=/usr,那么它会将东西安装在/usr/bin//usr/src下!”

所以我尝试了显而易见的方法:

  1. GOPATH=/usr添加到root的.bashrc文件中。
    它起作用了!
    有点。
    实际上并没有。
    事实证明,sudo不会执行root的.bashrc文件。出于“安全”或其他原因。

  2. /etc/sudoers中进行env_set或类似的操作。
    事实证明,/etc/sudoers只能删除环境变量。没有env_set指令。
    (据我所知)

  3. 查阅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:

  1. 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.

  2. Do env_set or something in /etc/sudoers
    Turns out, /etc/sudoers can only remove environment variables. There's no env_set directive.
    <sup>(As far as I can find)</sup>

  3. 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 &quot;GOPATH=/usr&quot; &gt;&gt; /etc/environment
sudo go get &lt;repo&gt;

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

huangapple
  • 本文由 发表于 2016年4月15日 22:35:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/36650052.html
匿名

发表评论

匿名网友

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

确定