duai1683 2015-08-20 05:55
浏览 81
已采纳

去运行找不到包

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条回答 默认 最新

  • douluan5523 2015-08-20 06:00
    关注

    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
                      ^^^^^^^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部