好好爱自己!

golang 的GOPATH设置的问题

go run footer.go 的时候呢, go会依次去GOPATH 和GOROOT设置的对应路径下面找对应的包(目录和文件)

找的时候呢, 会在GoPATH 和GOROOT对应的目录后面再加一层路径 "/src/"哦

---------------------------------------------------------------------------------------------

https://stackoverflow.com/questions/32110524/go-run-cannot-find-package

 

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?

-------------------------------------------------------------------------

 

3down voteaccepted

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
                  ^^^^^^^
shareedit
 
    
goloang doesn't look like the correct spelling. – VonC Aug 20 '15 at 6:04
    
Aah!! My eyes seem bad. It was really a stupid mistake. Another pair of eyes looking at it was helpful! It works now. – Bharat Aug 20 '15 at 6:06
    
@Bharat 11PM in SF... I understand ;) I've been there many times. – VonC Aug 20 '15 at 6:07 
posted @   立志做一个好的程序员  阅读(2880)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示