Golang模块:导入proto定义

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

Golang modules: Import proto definitions

问题

我正在尝试学习Go模块的依赖管理器,但在导入自动生成的proto定义时遇到了错误。我已经为此苦苦挣扎了两天。非常感谢任何帮助。

目录结构:

➜  ~/go-service-skeleton> tree .
.
├── protobuf
│     └── test_service
│         ├── test_service_config.pb.go
│         └── test_service_config.proto
├── src
│     └── test_service
│         ├── go.mod
│         ├── main.go
│         └── server
│             └── server.go

错误信息(最后一个错误尤其令人担忧):

➜  ~/go-service-skeleton/src/test_service go build
bootstrap.go:11:2: no required module provides package github.com/Sirupsen/logrus; to add it:
	go get github.com/Sirupsen/logrus
bootstrap.go:12:2: no required module provides package github.com/sirupsen/logrus; to add it:
	go get github.com/sirupsen/logrus
bootstrap.go:13:2: no required module provides package gopkg.in/yaml.v2; to add it:
	go get gopkg.in/yaml.v2
server/server.go:8:2: package protobuf/test_service is not in GOROOT
~/go-service-skeleton/src/test_service echo $GOPATH
/Users/****/go-service-skeleton:/Users/****/go-service-skeleton/protobuf
~/go-service-skeleton/src/test_service echo $GOROOT

英文:

I am trying to learn Go modules dependency manager but getting an error while importing auto-generated proto definitions. Have been struggling with this for 2 days. Any help will be greatly appreciated.
Directory structure:

➜  ~/go-service-skeleton> tree .
.
├── protobuf
│   └── test_service
│       ├── test_service_config.pb.go
│       └── test_service_config.proto
├── src
│   └── test_service
│       ├── go.mod
│       ├── main.go
│       └── server
│           └── server.go

Error (last error is particularly concerning):

➜  ~/go-service-skeleton/src/test_service go build
bootstrap.go:11:2: no required module provides package github.com/Sirupsen/logrus; to add it:
	go get github.com/Sirupsen/logrus
bootstrap.go:12:2: no required module provides package github.com/sirupsen/logrus; to add it:
	go get github.com/sirupsen/logrus
bootstrap.go:13:2: no required module provides package gopkg.in/yaml.v2; to add it:
	go get gopkg.in/yaml.v2
server/server.go:8:2: package protobuf/test_service is not in GOROOT
~/go-service-skeleton/src/test_service echo $GOPATH
/Users/****/go-service-skeleton:/Users/****/go-service-skeleton/protobuf
~/go-service-skeleton/src/test_service echo $GOROOT

答案1

得分: 2

在你的test_service模块中,go命令可见的唯一依赖项是在其go.mod文件中声明的依赖项。

最简单的修复方法是将你需要的所有源代码放在主模块中。请注意,根据https://blog.golang.org/generate的说明,“如果包含的包是为了被go get导入,一旦生成文件(并进行测试!),它必须被检入源代码仓库,以便客户端可以使用。”

英文:

Within your test_service module, the only dependencies visible to the go command are the ones declared in its go.mod file.

The simplest fix is to put all of the source code you need inside the main module. Note that per https://blog.golang.org/generate, “if the containing package is intended for import by go get, once the file is generated (and tested!) it must be checked into the source code repository to be available to clients.”

huangapple
  • 本文由 发表于 2021年6月4日 08:24:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/67830067.html
匿名

发表评论

匿名网友

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

确定