登录
首页 >  Golang >  Go问答

构建GoMobile项目时遇到“找不到包”错误

来源:stackoverflow

时间:2024-03-19 17:39:32 387浏览 收藏

在 Go 中使用 gomobile 构建 Android APK 时,可能会遇到“找不到包”错误。这是因为 Go 使用 GOPATH 或 GOROOT 环境变量中的路径引用,并且会从这些路径中的“src”目录查找包。提供绝对包路径不会被识别,因此必须使用 GOPATH 或 GOROOT 作为参考。

问题内容

我正在 go[1.12.9 windows/amd64] 中测试 gomobile 工具,并尝试将其附带的示例项目构建到 android apk 中。

将构建指向package目录并运行构建命令时,控制台给出找不到包错误。 go包如何被识别?

[注意-我尝试安装和使用gomobile工具,但它们也无法识别,我只能通过vscode将它们下载为git包]

PS D:\Script\Golang\bin> go version
go version go1.12.9 windows/amd64  
PS D:\Script\Golang\src\golang.org\x\mobile\example\basic> gci


    Directory: D:\Script\Golang\src\golang.org\x\mobile\example\basic


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       18-08-2019     11:27           4618 main.go
-a----       18-08-2019     11:27            225 main_x.go


PS D:\Script\Golang\src\golang.org\x\mobile\example\basic> cd D:\Script\Golang\bin
PS D:\Script\Golang\bin> .\gomobile.exe build D:\Script\Golang\src\golang.org\x\mobile\example\basic
D:\Script\Golang\bin\gomobile.exe: cannot find package "D:\\Script\\Golang\\src\\golang.org\\x\\mobile\\example\\basic" in any of:
        c:\go\src\D:\Script\Golang\src\golang.org\x\mobile\example\basic (from $GOROOT)
        D:\Script\Golang\src\D:\Script\Golang\src\golang.org\x\mobile\example\basic (from $GOPATH)
S D:\Script\Golang\bin> .\gomobile.exe build "D:\Script\Golang\src\golang.org\x\mobile\example\basic"
D:\Script\Golang\bin\gomobile.exe: cannot find package "D:\\Script\\Golang\\src\\golang.org\\x\\mobile\\example\\basic" in any of:
        c:\go\src\D:\Script\Golang\src\golang.org\x\mobile\example\basic (from $GOROOT)
        D:\Script\Golang\src\D:\Script\Golang\src\golang.org\x\mobile\example\basic (from $GOPATH)

解决方案


Go 采用环境/系统变量中提供的 GOPATH 或 GOROOT 路径的路径引用。它会在 GOPATH/GOROOT 中的 "src" 目录中查找包。这意味着提供绝对包路径将不起作用。

- 示例(以上案例)

GOPATH = D:\Script\Golang
GOROOT = C:\go
绝对包路径 = D:\Script\Golang\src\golang.org\x\mobile\example\basic

在这种情况下,提供绝对包路径将被读取为

GOPATH\D:\Script\Golang\src\golang.org\x\mobile\example\basic
GOROOT\D:\Script\Golang\src\golang.org\x\mobile\example\basic

由于golang使用GOPATH或GOROOT作为参考,所以包路径应该是

GOPATH\golang.org\x\mobile\example\basic

Golang 将自动在环境变量中使用引用 GOPATH,并将其后面的路径附加到它后面。
所以在上面的情况下,提供的包路径将是 -

PS D:\Script\Golang\bin> .\gomobile.exe 构建 golang.org\x\mobile\example\basic

好了,本文到此结束,带大家了解了《构建GoMobile项目时遇到“找不到包”错误》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>