doupa8922 2012-11-03 22:15 采纳率: 100%
浏览 2028
已采纳

为什么构建时“找不到包”(即使设置了GOPATH)?

即使我有 GOPATH 如果设置得当,我仍然无法获得“去构建”或“去运行”来找到我自己的软件包。我错什么了?

$ 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
  • 写回答

4条回答 默认 最新

  • dongliechuich10319 2012-11-04 22:26
    关注

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

    Additional recommended steps:

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大