奇怪的golang包导入问题

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

Strange golang package import issue

问题

这是目录树:

+/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| 导入但未使用:"client_test"
src/main.go|32| 未定义:client_test

我已经阅读了https://stackoverflow.com/questions/15049903/how-to-use-custom-packages-in-golang,我认为我有和这个人一样的解决方案,但我不知道如何解决这个问题。请帮忙。

go 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"(这正是我的工作目录)  
GOROOT="/usr/local/go"  
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"  
CGO_ENABLED="1"
英文:

Here is the directory tree:

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

In main.go:

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

In client_test.go:

package client_test
func Send() {
}

Error:

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

I've read https://stackoverflow.com/questions/15049903/how-to-use-custom-packages-in-golang and I think I've had the same solution like this guy, but I just don't know how to solve this problem. Please help.

go 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"  

答案1

得分: 11

> 命令 go,测试包。
>
> ... 文件名与文件模式“*_test.go”匹配的文件 ... 可以包含测试函数、基准函数和示例函数。

不要使用保留的名称。例如,将 client_test 替换为 clienttest

英文:

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

Don't use reserved names. For example, replace client_test with clienttest throughout.

答案2

得分: 1

好的,这是翻译好的部分:

好的,最后我找到了我的环境有什么问题:

因为我使用的是OS X,所以我使用.pkg文件来安装go,而GOROOT是"/usr/local/go"

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

仔细阅读官方文档后,我发现了这个:

Go二进制发行版假设它们将被安装在/usr/local/go(或Windows下的c:\Go)中,但也可以安装在其他位置。如果你这样做了,在使用Go工具时,你需要将GOROOT环境变量设置为该目录。

例如,如果你将Go安装到你的主目录,你应该在$HOME/.profile中添加以下命令:

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

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

这是我现在的~/.profile(已经更正):

export GOPATH=$HOME/projects/<我的项目文件夹>
export PATH=$PATH:$GOPATH/bin

顺便说一下:你不需要在工作区下创建/project文件夹。根据http://golang.org/doc/code.html#tmp_2,它也没有说你必须这样做:

工作区目录树现在看起来像这样:

bin/
    hello              # 命令可执行文件
pkg/
    linux_amd64/ 
        example/
            newmath.a  # 包对象
src/
    example/
        hello/
            hello.go   # 命令源码
        newmath/
            sqrt.go    # 包源码
英文:

OK finally I found what's wrong with my environment:

Since I'm using OS X so I used .pkg file to install go, and the GOROOT is "/usr/local/go"

Then I read another fake tutorial about GO installtion and it says I had to define GOROOT in my ~/.profile, so I added "GOROOT="/usr/local/go" inside ~/.profile, then everything went wrong.

After carefully read the official document I found this:

> 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

But the problem is, it did't mention what will happen if you add GOROOT in ~/.profile after .pkg installation, and it also didn't say you can't do this.

Here is my ~/.profile look like now (being corrected):

export GOPATH=$HOME/projects/<my project folder>
export PATH=$PATH:$GOPATH/bin

BTW: you don't NEED to make /project folder under workspace. According to http://golang.org/doc/code.html#tmp_2 , it also did't say you have to:

> 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

答案3

得分: 0

如果你的$GOPATH是"/Users/staff/projects/Minigame_Server",那么你的"project"的绝对路径应该是"/Users/staff/projects/Minigame_Server/src/project"。

你的导入语句应该是import "project/src/client_test"

或者,根据你似乎想要做的事情,假设你在"project"下有与Go相关的子目录"/pkg"和"/bin",将你的GOPATH设置为"/Users/staff/projects/Minigame_Server/project",

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

这里是(有些含糊不清,我同意)文档链接:http://golang.org/doc/code.html#tmp_2。我猜你已经阅读过它,这就是为什么你创建了/pkg、/bin和/src子目录,但问题在于GOPATH应该在哪里,然后是Go在查找导入时自动添加的隐式子级(/src会自动添加),然后是导入字符串本身。

英文:

If your $GOPATH is "/Users/staff/projects/Minigame_Server", your "project" absolute path should be "/Users/staff/projects/Minigame_Server/src/project".

Your import should then be import &quot;project/src/client_test&quot;.

Or, what you seem like you want to do, given that you have Go-related subdirectories "/pkg" and "/bin" under "project", set your GOPATH to "/Users/staff/projects/Minigame_Server/project"

and then you can import &quot;client_test&quot;. The basic idea is that Go will append the import string to $GOPATH/src/.

The (somewhat ambiguous, I agree) doc is here: http://golang.org/doc/code.html#tmp_2 . My guess is that you've read it which is why you've created the /pkg, /bin and /src subdirectories, but the catch is where GOPATH should be, then the implicit sublevel automatically added by Go when it looks for an import (/src is automatically added), then the import string as-is.

答案4

得分: -1

你可以使用以下代码在主函数中导入包,它将起作用
import(
"fmt"
"./client_test")
在主包中。

英文:

You can import the package in the main by using the following code it will work
import(
"fmt"
"./client_test")
in the main package

huangapple
  • 本文由 发表于 2013年3月14日 17:54:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/15405973.html
匿名

发表评论

匿名网友

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

确定