英文:
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.”
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论