Go install fails with error: no install location for directory xxx outside GOPATH

前端 未结 12 1692
醉话见心
醉话见心 2020-12-12 11:22
~/src/go-statsd-client> echo $GOPATH
/Users/me/gopath
~/src/go-statsd-client> echo $GOROOT
/usr/local/Cellar/go/1.1.1\\
~/src/go-statsd-client> go install
g         


        
相关标签:
12条回答
  • 2020-12-12 12:17

    On OSX Mojave 10.14, go is typically installed at /usr/local/go.

    Hence, setup these ENVs and you should be good to go.

    export GOPATH=/usr/local/go && export GOBIN=/usr/local/go/bin

    Also, add these to your bash_profile or zsh_profile if it works.

    echo "export GOPATH=/usr/local/go && export GOBIN=/usr/local/go/bin" >> ~/.bash_profile && source ~/.bash_profile

    0 讨论(0)
  • 2020-12-12 12:18

    I had this problem on Windows.

    My problem was that my %GOPATH% environment variable was set to

    C:\Users\john\src\goworkspace

    instead of

    C:\Users\john\src\goworkspace\

    Adding the missing trailing slash at the end fixed it for me.

    0 讨论(0)
  • 2020-12-12 12:18

    Careful when running

    export GOPATH=$HOME
    

    Go assume that your code exists in specific places related to GOPATH. So, instead, you can use docker to run any go command:

    docker run -it -v $(pwd):/go/src/github.com/<organization name>/<repository name> golang
    

    And now you can use any golang command, for example:

    go test github.com/<organization name>/<repository name> 
    
    0 讨论(0)
  • 2020-12-12 12:19

    For any OS X users and future me, you also need to set GOBIN to avoid this confusing message on install and go get

    mkdir bin 
    export GOBIN=$GOPATH/bin
    
    0 讨论(0)
  • 2020-12-12 12:19

    You need to setup both GOPATH and GOBIN. Make sure you have done the following (please replace ~/go with your preferred GOPATH and subsequently change GOBIN). This is tested on Ubuntu 16.04 LTS.

    export GOPATH=~/go 
    
    mkdir ~/go/bin
    
    export GOBIN=$GOPATH/bin
    

    The selected answer did not solve the problem for me.

    0 讨论(0)
  • 2020-12-12 12:20

    When you provide no arguments to go install, it defaults to attempting to install the package in the current directory. The error message is telling you that it cannot do that, because the current directory isn't part of your $GOPATH.

    You can either:

    • Define $GOPATH to your $HOME (export GOPATH=$HOME).
    • Move your source to within the current $GOPATH (mv ~/src/go-statsd-client /User/me/gopath).

    After either, going into the go-statsd-client directory and typing go install will work, and so will typing go install go-statsd-client from anywhere in the filesystem. The built binaries will go into $GOPATH/bin.

    As an unrelated suggestion, you probably want to namespace your package with a domain name, to avoid name clashing (e.g. github.com/you/go-statsd-client, if that's where you hold your source code).

    0 讨论(0)
提交回复
热议问题