Reputation: 5018
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"
Upvotes: 7
Views: 13814
Reputation: 65
You can import the package in the main by using the following code it will work import( "fmt" "./client_test") in the main package
Upvotes: -1
Reputation: 5018
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
Upvotes: 1
Reputation: 166569
... 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.
Upvotes: 11
Reputation: 23963
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 "project/src/client_test"
.
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 "client_test"
. 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.
Upvotes: 0