Golang:安装目录错误?

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

Golang: Installation directory error?

问题

我遇到了错误:

go install: 在GOPATH之外的目录/Users/xwilly/Dropbox/go/project/src没有安装位置

我在OS X上使用go版本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 install test.go
go install: 在GOPATH之外的目录/Users/xwilly/Dropbox/go/project/src没有安装位置

这是一个简单的示例:

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: 在GOPATH之外的目录/Users/xwilly/Dropbox/go/project/src/learning没有安装位置
英文:

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

答案1

得分: 32

你的目录结构是错误的。你试图安装一个命令(package main),它应该在一个以你的命令命名的源代码目录中。参考上面的 quux 命令。

在你的情况下,假设你的命令将被命名为 billy

$ mkdir -p /Users/xwilly/Dropbox/go/project/src/billy

这个目录是在你的 GOPATH 中。将你的 test.go 文件移动到这个目录中。运行

$ go install billy

除非你设置了 GOBIN,否则 billy 命令应该被安装在你的 GOPATH 中的

/Users/xwilly/Dropbox/go/project/bin

目录中,这个目录应该在你的 PATH 中。

英文:

> [Command go][1]
>
> [GOPATH environment variable][2]
>
> 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/ 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.
>
> 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 the quux 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 your test.go file to this directory. Run

$ go install billy

The command billy should, unless you have set GOBIN, be installed in the

/Users/xwilly/Dropbox/go/project/bin

directory inside your GOPATH, which should be in your PATH.
[1]: http://golang.org/cmd/go/
[2]: http://golang.org/cmd/go/#hdr-GOPATH_environment_variable

huangapple
  • 本文由 发表于 2013年7月8日 18:30:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/17524392.html
匿名

发表评论

匿名网友

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

确定