Go build:“找不到包”(即使设置了GOPATH)
技术问答
271 人阅读
|
0 人回复
|
2023-09-12
|
即使我已经GOPATH我仍然无法通过正确的设置go build”或“go run找到自己的包。我做错了什么?& |! C) e. o8 J: W5 L2 c
$ echo $GOROOT/usr/local/go$ echo $GOPATH/home/mitchell/go$ cat ~/main.gopackage mainimport "foobar"func main()$ cat /home/mitchell/go/src/foobar.gopackage foobar$ go build main.gomain.go:3:8: import "foobar": cannot find package
4 J- W9 |- z" B8 O0 m$ C5 g& c7 t ) ]7 \% r$ s. u" ^- F
解决方案: ( c7 k i% s% \6 }) t! m
它不起作用,因为你foobar.go源文件不在名foobar. go build并go install与源文件相比,尝试匹配目录。5 b! t) d& e3 E! P6 x
[ol]设置$GOPATH例如,有效目录export GOPATH="$HOME/go"
% X; Y( W9 Q# X: i移动foobar.go到$GOPATH/src/foobar/foobar.go并且施工应能正常工作。[/ol]其他推荐步骤:
. B, |$ q0 }6 W! \[ol]添加$GOPATH/bin到您$PATH的:PATH="$GOPATH/bin PATH"/ i2 n3 ^$ b# U
移动main.go子文件夹到 $GOPATH/src,例如$GOPATH/src/test1 Y0 m/ H v7 ]( N$ r7 \/ W/ z
go install test现在应该创建可执行文件$GOPATH/bin,可以通过test键入终端调用它。[/ol] |
|
|
|
|
|