Golang:安装目录错误? [英] Golang: Installation directory error?
问题描述
我有错误:
go install:no安装目录位置/ Users / xwilly / Dropbox / go / project / src outside GOPATH
我在OS X上使用版本1.1。
我可以建立&
我的环境:
GOPATH = / Users / xwilly / Dropbox / go / project
PATH = / Library / Frameworks / Python.framework / Versions / 2.7 / bin:/ usr / bin:/ bin:/ usr / sbin:/ sbin:/ usr / local / bin:/ usr / local / go / bin:/ Users / xwilly / Dropbox / go / project / bin
项目树:
/ Users / xwilly / Dropbox / go / project
bin
pkg
src
我可以毫无错误地构建:
..:src xwilly $ go build test.go
::src xwilly $ go安装test.go
go install:没有安装目录/ Users / xwilly / Dropbox / go / project / src在GOPATH之外的位置
这里是一个简单的例子:
xwilly $ cat test.go
package main
import(
fmt
)
func main(){
fmt.Println(Bonjour)
}
xwilly $ go run test.go
Bonjour
xwilly $ go安装测试。去
去安装:没有安装目录的位置/ Users / xwilly / Dropbox / go / project / src / learning 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 /斌/富/ QUUX
。foo /
被剥离,因此您
可以将DIR / bin
添加到PATH
来获取已安装的命令。如果设置了
GOBIN
环境变量,则将命令安装到它所指定的
目录,而不是DIR / bin < code $。
以下是一个目录布局示例:
GOPATH = / home / user / gocode
/ home / user / gocode /
src /
foo /
bar /(在软件包栏中输入代码)
x.go
quux /(在主包中去代码)
y.go
bin /
quux(已安装的命令)
pkg /
linux_amd64 /
foo /
bar.a(安装的包对象)
您的目录结构错误。您正试图安装一个命令( package main
)。它应该位于以命令命名的源目录中。请参阅上面的 quux
命令。
在您的情况下,假设您的命令将被命名为 billy
。
$ mkdir -p / Users / xwilly / Dropbox / go / project / src / billy
它位于 GOPATH
。将 test.go
文件移动到此目录。运行
$ go安装billy
命令 billy
应该被安装在中,除非你已经设置 GOBIN
/ p>
/ Users / xwilly / Dropbox / go / project / bin
$ p在
GOPATH
内的$ p>
目录,该目录应位于
PATH
。I have the error:
go install: no install location for directory /Users/xwilly/Dropbox/go/project/src outside GOPATH
I'm using go version 1.1 on OS X.
I can build & run but can't install packages.
My environment:
GOPATH=/Users/xwilly/Dropbox/go/project PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin:/Users/xwilly/Dropbox/go/project/bin
Project tree:
/Users/xwilly/Dropbox/go/project bin pkg src
I can build without error:
..:src xwilly$ go build test.go ..:src xwilly$ go install test.go go install: no install location for directory /Users/xwilly/Dropbox/go/project/src outside GOPATH
Here is a simple example:
xwilly$ cat test.go package main import ( "fmt" ) func main() { fmt.Println("Bonjour") } xwilly$ go run test.go Bonjour xwilly$ go install test.go go install: no install location for directory /Users/xwilly/Dropbox/go/project/src/learning outside 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 ofpkg
(pkg/GOOS_GOARCH
).If
DIR
is a directory listed in theGOPATH
, 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 inDIR/src/foo/quux
is installed into DIR/bin/quux, notDIR/bin/foo/quux
. Thefoo/
is stripped so that you can addDIR/bin
to yourPATH
to get at the installed commands. If theGOBIN
environment variable is set, commands are installed to the directory it names instead ofDIR/bin
.Here's an example directory layout:
GOPATH=/home/user/gocode /home/user/gocode/ 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)
Your directory structure is wrong. You are trying to install a command (
package main
). It should be in a source directory named after your command. See thequux
command above.In your case, assume your command is going to be named
billy
.$ mkdir -p /Users/xwilly/Dropbox/go/project/src/billy
which is inside your
GOPATH
. Move yourtest.go
file to this directory. Run$ go install billy
The command
billy
should, unless you have setGOBIN
, be installed in the/Users/xwilly/Dropbox/go/project/bin
directory inside your
GOPATH
, which should be in yourPATH
.这篇关于Golang:安装目录错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!