doudi8298
doudi8298
2016-04-15 14:35
浏览 111
已采纳

相当于npm install -g的Golang

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!

图片转代码服务由CSDN问答提供 功能建议

如果我要安装一个编译的Golang程序,以便可以在任何地方使用bash命令运行它 在我的计算机上,我该怎么做? 例如,在nodejs

  npm install -g express 
   
 
 

安装express这样我就可以运行命令

 表达myapp 
   
 
 

并express将为名为“ myapp”的节点应用程序生成文件目录 我当前的目录是。 有等效的命令吗? 我相信现在使用“执行安装”命令,您必须在包含可执行文件的目录中才能运行它

谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duan5362
    duan5362 2016-04-15 14:42
    已采纳

    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"
    

    Alternatively, you could also copy or link the executables to /usr/local/bin:

    ln -s $GOPATH/bin/some-binary /usr/local/bin/some-binary
    
    点赞 评论
  • dongqiuqiu4736
    dongqiuqiu4736 2016-04-15 14:41

    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.

    点赞 评论
  • dongnao2582
    dongnao2582 2018-10-11 16:09

    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.

    点赞 评论
  • doujingxi3356
    doujingxi3356 2019-07-10 19:36

    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.
      (As far as I can find)

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

    点赞 评论

相关推荐