简体   繁体   中英

Go build cannot find package

Problem

I haven't been able to find a solution to this by looking at related questions. I can't tell what makes my Go environment different from the canonical setup.

go env returns

GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="386"
GOCHAR="8"
GOOS="linux"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread"
CGO_ENABLED="1"

tree $GOPATH returns

/home/USER/go
├── bin
├── pkg
│   └── linux_386
│       └── bitbucket.org
│           └── USER-NAME
│               └── PROJECT
│                   └── my_package.a
└── src
    └── bitbucket.org
        └── USER-NAME
            └── PROJECT
                ├── main
                │   ├── main.go
                └── my_package
                    └── my_package.go

(ALL-CAPS are substitutions)

main.go contains

package main

import (
        "bitbucket.org/USER-NAME/PROJECT/my_package"
)

func main() {
        my_package.Foo()
}

Calling go build in the main directory returns import "my_package": cannot find package

Volker pointed out that go env should have returned a GOPATH entry as well. The source of the env command corroborates that. However, running echo $GOPATH in bash or os.Getenv("GOPATH") in Go both return \\home\\USER\\go . I'm not sure why the same isn't returned by go env .

Solution

I was running Go 1.0 when I was having this issue. The problem disappeared when I upgraded to Go 1.2.1.

You have a directory called main. This won't work. Change it.

Structure it like $GOPATH/src/bitbucket.com/youruser name/yourpackagename/{main.go, otherthing.go, otherpackagedirectory} .

"package main" doesn't have to be in it's own sub folder: it inherits the name of your Bitbucket project (username/myprojectname).

You did not set (or export) GOPATH . GOPATH Is much more important than GOROOT (at least in newe Go versions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM