本文介绍了去构建:“找不到包"(即使设置了 GOPATH)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即使我已经正确设置了 GOPATH
,我仍然无法通过go build"或go run"来找到我自己的包.我做错了什么?
Even though I have GOPATH
properly set, I still can't get "go build" or "go run" to find my own packages. What am I doing wrong?
$ echo $GOROOT
/usr/local/go
$ echo $GOPATH
/home/mitchell/go
$ cat ~/main.go
package main
import "foobar"
func main() { }
$ cat /home/mitchell/go/src/foobar.go
package foobar
$ go build main.go
main.go:3:8: import "foobar": cannot find package
推荐答案
它不起作用,因为您的 foobar.go
源文件不在名为 foobar
的目录中.go build
和 go install
尝试匹配目录,而不是源文件.
It does not work because your foobar.go
source file is not in a directory called foobar
. go build
and go install
try to match directories, not source files.
- 将
$GOPATH
设置为有效目录,例如导出 GOPATH="$HOME/go"
- 将
foobar.go
移动到$GOPATH/src/foobar/foobar.go
并且构建应该可以正常工作.
- Set
$GOPATH
to a valid directory, e.g.export GOPATH="$HOME/go"
- Move
foobar.go
to$GOPATH/src/foobar/foobar.go
and building should work just fine.
其他推荐步骤:
- 将
$GOPATH/bin
添加到您的$PATH
中:PATH="$GOPATH/bin:$PATH"
- 将
main.go
移动到$GOPATH/src
的子文件夹,例如$GOPATH/src/test
go install test
现在应该在$GOPATH/bin
中创建一个可执行文件,可以通过在终端中键入test
来调用它.莉>
- Add
$GOPATH/bin
to your$PATH
by:PATH="$GOPATH/bin:$PATH"
- Move
main.go
to a subfolder of$GOPATH/src
, e.g.$GOPATH/src/test
go install test
should now create an executable in$GOPATH/bin
that can be called by typingtest
into your terminal.
这篇关于去构建:“找不到包"(即使设置了 GOPATH)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!