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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行