2013-03-14 128 views
5

这里是目录树:奇怪golang包导入问题

+/project 
    +---/bin 
    +---/pkg 
    +---/src 
    +---/client_test 
     +---client_test.go 
    +---main.go 

在main.go:

package main 
import ("client_test") 
func main() { 
    client_test.Send() 
} 

在client_test.go:

package client_test 
func Send() { 
} 

错误:

src/main.go|8| imported and not used: "client_test" 
src/main.go|32| undefined: client_test 

我读过How to use custom packages in golang?,我想我已经有了像这个人一样的解决方案,但我不知道如何解决这个问题。请帮忙。

去ENV:

GOARCH="amd64" 
GOBIN="/usr/local/go/bin" 
GOCHAR="6" 
GOEXE="" 
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common" 
GOHOSTARCH="amd64" 
GOHOSTOS="darwin" 
GOOS="darwin" 
GOPATH="/Users/staff/projects/Minigame_Server" (that's exactly my working directory) 
GOROOT="/usr/local/go" 
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" 
CGO_ENABLED="1" 
+0

什么是您的$ GOPATH,或者更好的输出'go env'? – zzzz 2013-03-14 10:03:08

+0

'import'尝试从它的'$ GOPATH'中提取的每个“工作空间”开始寻找指定的路径。从你的树来看,我会说你的'$ GOPATH'必须包含'/ some/path/to/project',也就是说它必须包含那个“project”目录。这是否适合您的情况? – kostix 2013-03-14 10:16:09

+0

OARCH = “AMD64” GOBIN = “/ USR /本地/去/ BIN” GOCHAR = “6” GOEXE = “” GOGCCFLAGS = “ - 克-O2 -fPIC -m64 -pthread -fno-常见的” GOHOSTARCH = “AMD64” GOHOSTOS = “达尔文” GOOS = “达尔文” GOPATH = “/用户/员工/项目/ Minigame_Server”(这正是我的工作目录) GOROOT = “在/ usr /本地/去” GOTOOLDIR =“/ usr/local/go/pkg/tool/darwin_amd64” CGO_ENABLED =“1” – 2013-03-14 10:37:49

回答

11

Command go, Test packages.

... files with names matching the file pattern "*_test.go" ... can contain test functions, benchmark functions, and example functions.

请勿使用保留名称。例如,在整个过程中将client_test替换为clienttest

+1

经过一些测试,这是正确的解决方案,谢谢! PS:软件包名称可以包含“_test”,但文件名不能。 – 2013-03-15 02:39:34

0

如果你的$ GOPATH是 “/用户/员工/项目/ Minigame_Server”,你的 “项目” 绝对路径应该是“/用户/员工/项目/ Minigame_Server/src目录/项目”。

您的进口应该是import "project/src/client_test"。或者,假如你在“project”下有Go-related子目录“/ pkg”和“/ bin”,将你的GOPATH设置为“/ Users/staff/projects/Minigame_Server”/project“

然后你可以import "client_test"。基本思想是Go会将导入的字符串追加到$ GOPATH/src /中。

(有点含糊,我同意)doc在这里:http://golang.org/doc/code.html#tmp_2。我的猜测是你已经读过了,这就是为什么你创建了/ pkg,/ bin和/ src子目录,但catch是GOPATH应该在的地方,然后Go在查找一个import时自动添加了隐式sublevel (/ src自动添加),然后按照原样输入字符串。

1

OK终于让我找到有什么错我的环境:

由于我使用的OS X,所以我用pkg文件安装去,而GOROOT是“在/ usr /本地/ GO”

然后我读了另一个关于GO installtion的假教程,它说我必须在我的〜/ .profile中定义GOROOT,所以我在〜/ .profile里面添加了“GOROOT =”/ usr/local/go“,然后一切都出错了。

经过仔细阅读官方文档,我发现这个:

The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install them in a different location. If you do this, you will need to set the GOROOT environment variable to that directory when using the Go tools.

For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:

export GOROOT=$HOME/go export PATH=$PATH:$GOROOT/bin

但问题是,它没有提到如果在.pkg安装后在〜/ .profile中添加GOROOT会发生什么情况,也没有说你不能这样做。

这是我的〜/。轮廓看起来像现在(被纠正):

出口GOPATH = $ HOME /项目/ 出口PATH = $ PATH:$ GOPATH/bin中

BTW:你不需要做/项目下的工作区文件夹。据http://golang.org/doc/code.html#tmp_2,还我以前不说,你应该:

The workspace directory tree now looks like this:

bin/ 
    hello    # command executable 
pkg/ 
    linux_amd64/ 
     example/ 
      newmath.a # package object 
src/ 
    example/ 
     hello/ 
      hello.go # command source 
     newmath/ 
      sqrt.go # package source 
+2

你在混合GOROOT和GOPATH吗? – Alexander 2013-08-08 12:47:46

-1

您可以通过使用下面的代码在主导入的包,将工作 进口( “FMT” ”。/client_test“) 在主包装中