golang 的GOPATH设置的问题

本文解答了一位开发者在使用Go语言时遇到的问题:在正确设置了GOPATH的情况下,尝试运行一个包含本地包依赖的程序时遇到了找不到包的错误。通过检查发现,问题在于路径设置中的拼写错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/oxspirt/p/7224248.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值