Go run无法找到包。

huangapple go评论76阅读模式
英文:

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
                  ^^^^^^^

huangapple
  • 本文由 发表于 2015年8月20日 13:55:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/32110524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定