2013-07-08 110 views
21

我有錯誤:在OS XGolang:安裝目錄錯誤?

go install: no install location for directory /Users/xwilly/Dropbox/go/project/src outside GOPATH 

我用去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: no install location for directory /Users/xwilly/Dropbox/go/project/src outside GOPATH 

下面是一個簡單的例子:

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 
+0

test.go中的軟件包名稱是什麼? – thwd

+0

包名是=>包主 – Xwilly

+0

您不能安裝'package main'。閱讀[如何寫代碼](http://golang.org/doc/code.html)。 – thwd

回答

31

Command go

GOPATH environment variable

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) 

你的目錄結構是錯誤的。您正嘗試安裝一個命令(package main)。它應該位於以命令命名的源目錄中。請參閱上面的quux命令。你的命令將會被命名爲billy

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

這是你的GOPATH裏面。將您的test.go文件移至此目錄。運行

$ go install billy 

命令billy應該,除非你已經設置GOBIN,安裝在

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

目錄中GOPATH,這應該是在你的PATH內。

+1

感謝彼得,爲清楚的解釋:)。 – Xwilly

+1

+1感謝您的好解釋。我錯過了文檔中的「gobin」位 – Rippo