去构建:“找不到包"(即使设置了 GOPATH) [英] Go build: "Cannot find package" (even though GOPATH is set)

查看:47
本文介绍了去构建:“找不到包"(即使设置了 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 buildgo 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.

  1. $GOPATH 设置为有效目录,例如导出 GOPATH="$HOME/go"
  2. foobar.go 移动到 $GOPATH/src/foobar/foobar.go 并且构建应该可以正常工作.
  1. Set $GOPATH to a valid directory, e.g. export GOPATH="$HOME/go"
  2. Move foobar.go to $GOPATH/src/foobar/foobar.go and building should work just fine.

其他推荐步骤:

  1. $GOPATH/bin 添加到您的 $PATH 中:PATH="$GOPATH/bin:$PATH"
  2. main.go 移动到 $GOPATH/src 的子文件夹,例如$GOPATH/src/test
  3. go install test 现在应该在 $GOPATH/bin 中创建一个可执行文件,可以通过在终端中键入 test 来调用它.莉>
  1. Add $GOPATH/bin to your $PATH by: PATH="$GOPATH/bin:$PATH"
  2. Move main.go to a subfolder of $GOPATH/src, e.g. $GOPATH/src/test
  3. go install test should now create an executable in $GOPATH/bin that can be called by typing test into your terminal.

这篇关于去构建:“找不到包"(即使设置了 GOPATH)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆