英文:
Go run cannot find package
问题
我有一个基于这篇文章的示例项目设置。
这是我的目录结构:
goloang/
└── fooproject
└── src
├── foo
│ └── foo.go
└── fooer
└── fooer.go
我已经设置了GOTPATH
。
$ echo $GOPATH
/Users/Bharat/Dev/golang/fooproject
我试图运行foor.go
,但是我得到一个找不到包的错误,尽管它存在于GOPATH
指定的位置,即src/foo
。
这是我的运行方式:
$ pwd
/Users/Bharat/Dev/goloang/fooproject/src/fooer
$ go run fooer.go
fooer.go:4:3: cannot find package "foo" in any of:
/usr/local/go/src/foo (from $GOROOT)
/Users/Bharat/Dev/golang/fooproject/src/foo (from $GOPATH)
以下是文件内容。
src/foo/foo.go
:
package foo
import (
"fmt"
)
func Bar() {
fmt.Println("bar")
}
src/fooer/fooer.go
:
package main
import (
"foo"
)
func main() {
foo.Bar()
}
当我尝试运行go build fooer.go
时,我得到相同的错误。
我在项目结构上出了什么问题?
英文:
I have a sample project setup based on this article.
This is my directory structure:
goloang/
└── fooproject
   └── src
   ├── foo
   │   └── foo.go
   └── fooer
   └── fooer.go
I have set the GOTPATH
$ echo $GOPATH
/Users/Bharat/Dev/golang/fooproject
I am trying to run foor.go
but I get a cannot find package error even though it exists in the location referenced by GOPATH
i.e. in src/foo
.
This is how I am running it:
$ pwd
/Users/Bharat/Dev/goloang/fooproject/src/fooer
$ go run fooer.go
fooer.go:4:3: cannot find package "foo" in any of:
/usr/local/go/src/foo (from $GOROOT)
/Users/Bharat/Dev/golang/fooproject/src/foo (from $GOPATH)
These are the file contents.
src/foo/foo.go
:
package foo
import (
"fmt"
)
func Bar() {
fmt.Println("bar")
}
src/fooer/fooer.go
:
package main
import (
"foo"
)
func main() {
foo.Bar()
}
I get the same error when I try to do go build fooer.go
.
Where am I going wrong with the project structure?
答案1
得分: 5
可能是因为拼写错误:
goloang
而不是
golang
你的设置是:
$ echo $GOPATH
/Users/Bharat/Dev/golang/fooproject
^^^^^^
$ pwd
/Users/Bharat/Dev/goloang/fooproject/src/fooer
^^^^^^^
英文:
That might be because of a typo:
goloang
instead of
golang
You have:
$ echo $GOPATH
/Users/Bharat/Dev/golang/fooproject
^^^^^^
$ pwd
/Users/Bharat/Dev/goloang/fooproject/src/fooer
^^^^^^^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论