Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange golang package import issue

Tags:

go

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 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"  
like image 253
Reck Hou Avatar asked Mar 14 '13 09:03

Reck Hou


2 Answers

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.

like image 58
peterSO Avatar answered Sep 19 '22 20:09

peterSO


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/ 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
like image 35
Reck Hou Avatar answered Sep 22 '22 20:09

Reck Hou