安装Go时出现错误:在GOPATH之外的目录xxx没有安装位置。

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

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

问题

~/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
go install: 在GOPATH之外的目录/Users/me/src/go-statsd-client没有安装位置

无论项目的结构如何,始终会出现相同的错误消息。go build可以正常工作。

这是我的go env

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/me/gopath"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.1.1"
GOTOOLDIR="/usr/local/Cellar/go/1.1.1/pkg/tool/darwin_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
CGO_ENABLED="1"

这是在Mac OSX Mountain Lion上安装的go,并且使用了homebrew。

英文:
~/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
go install: no install location for directory /Users/me/src/go-statsd-client outside GOPATH

No matter what structure the project is in this always fails with the same message. Go build works perfectly.

Here is my go env

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/me/gopath"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.1.1"
GOTOOLDIR="/usr/local/Cellar/go/1.1.1/pkg/tool/darwin_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
CGO_ENABLED="1"

This is on Mac OSX Mountain Lion and go was installed with homebrew.

答案1

得分: 212

对于任何OS X用户和未来的我,你还需要设置GOBIN以避免在安装和go get时出现这个令人困惑的消息。

mkdir bin
export GOBIN=$GOPATH/bin
英文:

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

答案2

得分: 119

当你在go install命令中不提供任何参数时,默认会尝试将包安装在当前目录中。错误消息告诉你它无法这样做,因为当前目录不是你的$GOPATH的一部分。

你可以选择:

  • $GOPATH定义为你的$HOMEexport GOPATH=$HOME)。
  • 将你的源代码移动到当前$GOPATH内(mv ~/src/go-statsd-client /User/me/gopath)。

在执行上述操作之后,进入go-statsd-client目录并输入go install命令,它将正常工作,无论你在文件系统的任何位置输入go install go-statsd-client命令都将正常工作。生成的二进制文件将放在$GOPATH/bin目录中。

作为一个无关的建议,你可能希望使用域名对你的包进行命名空间,以避免名称冲突(例如,如果你的源代码存放在github.com/you/go-statsd-client中)。

英文:

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

答案3

得分: 30

你正在使用go install命令来安装一个位于GOPATH文件夹之外的目录。
请设置你的GOBIN环境变量,或将src文件夹移动到GOPATH内。

GOPATH/
     bin/
     src/
       go-statsd-client/

更多信息:GO BUILD源代码,第296行

英文:

You are using go install on a directory outside the GOPATH folder.
Set your GOBIN env variable, or move src folder inside GOPATH.

GOPATH/
     bin/
     src/
       go-statsd-client/

More info: GO BUILD Source code, line 296

答案4

得分: 27

你需要设置GOPATHGOBIN。确保你已经完成了以下步骤(请将~/go替换为你喜欢的GOPATH,并相应地更改GOBIN)。这在Ubuntu 16.04 LTS上经过测试。

export GOPATH=~/go 

mkdir ~/go/bin

export GOBIN=$GOPATH/bin

选定的答案对我来说没有解决问题。

英文:

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.

答案5

得分: 7

你需要在你选择的GOPATH目录下创建3个目录。

GOPATH
     /bin
     /src
       /someProgram
        program.go
       /someLibrary
        library.go
     /pkg

然后你可以在someProgram目录(将可执行文件放在bin目录中)或someLibrary目录(将库文件放在pkg目录中)内运行go install命令。

英文:

You'll want to have 3 directories inside your chosen GOPATH directory.

GOPATH
     /bin
     /src
       /someProgram
        program.go
       /someLibrary
        library.go
     /pkg

Then you'll run go install from inside either someProgram (which puts an executable in bin) or someLibrary (which puts a library in pkg).

答案6

得分: 3

我在Windows上遇到了这个问题。

我的问题是我的%GOPATH%环境变量被设置为

C:\Users\john\src\goworkspace

而不是

C:\Users\john\src\goworkspace\

在末尾添加缺失的斜杠解决了我的问题。

英文:

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.

答案7

得分: 3

以下是我在Mac上使用Atom安装go并配置好的.bash_profile文件:

export GOROOT=`go env GOROOT`
export GOPATH=/Users/yy/Projects/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
英文:

For what it's worth, here's my .bash_profile, that works well for me on a mac with Atom, after installing go with Homebrew:

export GOROOT=`go env GOROOT`
export GOPATH=/Users/yy/Projects/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

答案8

得分: 2

在我的情况下(OS X),这是因为我将GOPATH设置为/home/username/go(按照书上的说明),而不是/Users/username/go

英文:

In my case (OS X) it was because I have set GOPATH to /home/username/go (as per the book) instead of /Users/username/go

答案9

得分: 2

我在Windows上使用命令go help gopath在cmd中获取了相关信息,并阅读了指令中的粗体文本。

如果你想要安装的代码位于..BaseDir...\SomeProject\src\basic\set,那么GOPATH不应该与代码位于同一位置,而应该只是基本项目目录:..BaseDir...\SomeProject

GOPATH环境变量列出了查找Go代码的位置。在Unix上,值是以冒号分隔的字符串。在Windows上,值是以分号分隔的字符串。在Plan 9上,值是一个列表。

如果未设置环境变量,GOPATH默认为用户主目录中名为"go"的子目录(在Unix上为$HOME/go,在Windows上为%USERPROFILE%\go),除非该目录包含Go发行版。运行"go env GOPATH"可以查看当前的GOPATH。

请参阅https://golang.org/wiki/SettingGOPATH以设置自定义的GOPATH。

GOPATH中列出的每个目录必须具有规定的结构:

  • src目录保存源代码。src下的路径确定了导入路径或可执行文件名。

  • pkg目录保存已安装的包对象。与Go树一样,每个目标操作系统和体系结构对都有自己的pkg子目录(pkg/GOOS_GOARCH)。

  • 如果DIR是GOPATH中列出的目录,具有DIR/src/foo/bar中源代码的包可以作为"foo/bar"导入,并将其编译形式安装到"DIR/pkg/GOOS_GOARCH/foo/bar.a"。

  • bin目录保存已编译的命令。每个命令的名称都是其源目录,但只包括最后一个元素,而不是整个路径。也就是说,具有DIR/src/foo/quux中源代码的命令将安装到DIR/bin/quux,而不是DIR/bin/foo/quux。"foo/"前缀被删除,以便您可以将DIR/bin添加到PATH中以访问已安装的命令。如果设置了GOBIN环境变量,命令将安装到其指定的目录,而不是DIR/bin。GOBIN必须是绝对路径。

以下是一个示例目录布局:

GOPATH=/home/user/go

/home/user/go/
    src/
        foo/
            bar/               (包bar中的Go代码)
                x.go
            quux/              (主包中的Go代码)
                y.go
    bin/
        quux                   (已安装的命令)
    pkg/
        linux_amd64/
            foo/
                bar.a          (已安装的包对象)

..........

如果已将GOPATH设置为基本项目目录并仍然遇到此问题,在Windows上,你可以尝试将GOBIN设置为Base Project DIR\bin%GOPATH%\bin

英文:

I'm on Windows, and I got it by giving command go help gopath to cmd, and read the bold text in the instruction,

that is if code you wnat to install is at ..BaseDir...\SomeProject\src\basic\set, the GOPATH should not be the same location as code, it should be just Base Project DIR: ..BaseDir...\SomeProject.

> The GOPATH environment variable lists places to look for Go code. On
> Unix, the value is a colon-separated string. On Windows, the value is
> a semicolon-separated string. On Plan 9, the value is a list.
>
> If the environment variable is unset, GOPATH defaults to a
> subdirectory named "go" in the user's home directory ($HOME/go on
> Unix, %USERPROFILE%\go on Windows), unless that directory holds a Go
> distribution. Run "go env GOPATH" to see the current GOPATH.
>
> See https://golang.org/wiki/SettingGOPATH to set a custom GOPATH.
>
> Each directory listed in GOPATH must have a prescribed structure:
>
> The src directory holds source code. The path below src determines the
> import path or executable name.
>
> The pkg directory holds installed package objects. As in the Go tree,
> each target operating system and architecture pair has its own
> subdirectory of pkg (pkg/GOOS_GOARCH).
>
> If DIR is a directory listed in the GOPATH, a package with source in
> DIR/src/foo/bar can be imported as "foo/bar" and has its compiled form
> installed to "DIR/pkg/GOOS_GOARCH/foo/bar.a".

>
> The bin directory holds compiled commands. Each command is named for
> its source directory, but only the final element, not the entire path.
> That is, the command with source in DIR/src/foo/quux is installed into
> DIR/bin/quux, not DIR/bin/foo/quux. The "foo/" prefix is stripped so
> that you can add DIR/bin to your PATH to get at the installed
> commands. If the GOBIN environment variable is set, commands are
> installed to the directory it names instead of DIR/bin. GOBIN must be
> an absolute path.
>
> Here's an example directory layout:
>
> GOPATH=/home/user/go
>
> /home/user/go/
> src/
> foo/
> bar/ (go code in package bar)
> x.go
> quux/ (go code in package main)
> y.go
> bin/
> quux (installed command)
> pkg/
> linux_amd64/
> foo/
> bar.a (installed package object)
> ..........

if GOPATH has been set to Base Project DIR and still has this problem, in windows you can try to set GOBIN as Base Project DIR\bin or %GOPATH%\bin.

答案10

得分: 1

小心运行

export GOPATH=$HOME

Go假设你的代码存在于与GOPATH相关的特定位置。因此,你可以使用Docker来运行任何Go命令:

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

现在你可以使用任何Go命令,例如:

go test github.com/<organization name>/<repository name>
英文:

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/&lt;organization name&gt;/&lt;repository name&gt; golang

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

go test github.com/&lt;organization name&gt;/&lt;repository name&gt; 

答案11

得分: 0

在Windows中,当我设置GOPATH环境变量时,我的cmd窗口已经打开。
首先,我必须关闭cmd窗口,然后重新打开才能生效。

英文:

In windows, my cmd window was already open when I set the GOPATH environment variable.
First I had to close the cmd and then reopen for it to become effective.

答案12

得分: -1

在OSX Mojave 10.14上,go通常安装在/usr/local/go目录下。

因此,设置这些环境变量,你就可以开始使用了。

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

如果可行的话,还可以将这些添加到你的bash_profilezsh_profile中。

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

英文:

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 &amp;&amp; export GOBIN=/usr/local/go/bin

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

echo &quot;export GOPATH=/usr/local/go &amp;&amp; export GOBIN=/usr/local/go/bin&quot; &gt;&gt; ~/.bash_profile &amp;&amp; source ~/.bash_profile

huangapple
  • 本文由 发表于 2013年8月9日 22:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/18149601.html
匿名

发表评论

匿名网友

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

确定