The problem of importing a strange golang

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 read How to use custom packages in golang? , and I think I had the same solution as 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 exactly my working directory)  
GOROOT="/usr/local/go"  
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"  
CGO_ENABLED="1"  
+5
source share
4 answers

Go command, test packages.

... files with names that match the file template "* _test.go" ... may contain test functions, control functions, and sample functions.

Do not use reserved names. For example, replace client_testwith clienttest.

+11
source

OK, finally, I found what is wrong in my environment:

OS X, .pkg go, GOROOT - "/usr/local/go"

GO, , GOROOT ~/.profile, "GOROOT =" /usr/local/go " ~/.profile, .

:

Go , /usr/local/go ( c:\Go Windows), . , GOROOT Go.

, Go , $HOME/.profile:

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

, , , GOROOT ~/.profile .pkg, , .

~/.profile ():

export GOPATH = $HOME/projects/ export PATH = $PATH: $GOPATH/bin

: /project . http://golang.org/doc/code.html#tmp_2, , :

:

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

$GOPATH "/Users/staff/projects/Minigame_Server", "/Users/staff/projects/Minigame_Server/src/project".

import "project/src/client_test".

, , , , Go-related "/pkg" "/bin" "project", GOPATH "/Users/staff/projects/Minigame_Server/"

import "client_test". , Go $GOPATH/src/.

(somewhat controversial, I agree) the doc is here: http://golang.org/doc/code.html#tmp_2 . I assume you read it, so you created the subdirectories / pkg, / bin and / src, but catch is where GOPATH should be, and then the implicit sublevel automatically added by Go when it searches for imports (/ src is automatically added ), then the import string is as-is.

0
source

You can import the package mainly using the following code that it will work Import ("FMT" "./client_test") in the main package

-1
source

All Articles