无法导入包 “google.golang.org/api/compute/v1″。

huangapple go评论76阅读模式
英文:

Cannot import package "google.golang.org/api/compute/v1"

问题

我正在使用云函数自动创建一些虚拟机实例。之前,我在我的用例中取得了成功。你可以在这里查看。现在,我想要更新元数据脚本中的一些代码行。但是当我保存代码时,它告诉我一些我导入的包找不到。日志如下:

构建失败:src/cloudfunctions/function.go:9:2: 找不到包 "google.golang.org/api/compute/v1",在以下任何位置都找不到:
/usr/local/go/src/google.golang.org/api/compute/v1(来自 $GOROOT)
/workspace/src/google.golang.org/api/compute/v1(来自 $GOPATH);错误 ID:2f5e35a0

但是我在https://pkg.go.dev/google.golang.org/api/compute/v1中查看了文档,使用示例导入方式如下 import "google.golang.org/api/compute/v1",和我的代码一样。所以,谁能告诉我在云函数中的 $GOROOT 和 $GOPATH 是什么,以及如何导入这个包。

提前感谢。

英文:

I am using cloud function to auto create some VM instance. Before, I was success in my use-case. You can view it here.Now, I want to update some line in metadata script. But when I save the code, it tell me some package I was import cannot find. Logs is

Build failed: src/cloudfunctions/function.go:9:2: cannot find package "google.golang.org/api/compute/v1" in any of:
/usr/local/go/src/google.golang.org/api/compute/v1 (from $GOROOT)
/workspace/src/google.golang.org/api/compute/v1 (from $GOPATH); Error ID: 2f5e35a0

But I was view document in https://pkg.go.dev/google.golang.org/api/compute/v1, usage example import like this import "google.golang.org/api/compute/v1", as same as me.
So, who can tell me what is $GOROOT and $GOPATH in cloud function, and how can I import this package.

Thanks in advance.

答案1

得分: 1

运行以下命令:

go get -u -x google.golang.org/api/compute/v1

然后在终端中切换到你的目录,运行以下命令:

go mod init filename.go

然后运行:

go mod tidy

还需要重新启动你的IDE,以便更新。

英文:

Run

go get -u -x google.golang.org/api/compute/v1

Then cd/open your directory in terminal then run
go mod init filename.go
then
go mod tidy
Also restart your IDE as that will need to update

huangapple
  • 本文由 发表于 2022年5月9日 12:09:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/72166917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定