go env 關鍵數據是這樣的
GOPATH="/home/zzy/goProject" GOROOT="/usr/local/go"
項目目錄是這樣的
goProject
├── bin
├── pkg
└── src
└── go_learn
└── day01
├── hello
│ ├── hello
│ └── hello.go
└── str_type
├── str.go
└── world.go
hello.go我的代碼是這樣的
package main import ( "fmt" "go_learn/day01/str_type/world" ## 這句導入包,一直報錯 ) func main() { fmt.Print("hello world") test() }
world.go代碼是這樣的
package main import "fmt" func test() { fmt.Print("啦啦啦啦") }
go build 報錯是這樣的
$ go build hello.go:5:2: cannot find package "go_learn/day01/str_type/world" in any of: /usr/local/go/src/go_learn/day01/str_type/world (from $GOROOT) /home/zzy/goProject/src/go_learn/day01/str_type/world (from $GOPATH)
最后解決了!十分low的一個問題,估計初學者才會犯
world.go文件內容改一下就可以
package world # 是個文件,不是包,不要導入main import "fmt" # 函數名大寫,就可以在外面引用該函數 func Test() { fmt.Println("lll") }
