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

报告相同问题?

悬赏问题

  • ¥15 vue2(标签-chrome|关键词-浏览器兼容)
  • ¥15 python网络流自动生成系统 医学领域
  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?