gonpmbinnpm-installgoinstall

Golang equivalent of npm install -g


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!


Solution

  • 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