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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM