Search code examples
goprotocol-buffersgo-modules

Golang modules: Import proto definitions


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


Solution

  • 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.”