How can I resolve external package dependencies in Go generated by protobuf?


I am following a guide on creating and using protocol buffers and gRPC for Golang. I have got up to the point where I have the generated Go files in an output directory, but I am faced with a few issues:

The imports in all 3 files start like this:

// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
//  protoc-gen-go v1.25.0
//  protoc        v3.15.5
// source: building.proto

package location

import (
    proto ""
    protoreflect ""
    protoimpl ""
    reflect "reflect"
    sync "sync"

Now when I try to build or run my main.go file, which just imports the buffers and generates a message with them (not that it even gets past the import stage), the following happens:

..\proto\out\loc\location\building.pb.go:10:2: cannot find package
..\proto\out\loc\location\building.pb.go:11:2: cannot find package
..\proto\out\loc\location\building.pb.go:12:2: cannot find package

Those lines are referring to the first 3 imports in the previous code snippet.

I can’t seem to figure out why this is happening. Below I will describe the steps I have taken to try to resolve this:

  1. Reinstall protoc and the protoc-gen-go compiler (multiple times in both local user dirs and system dirs)
  2. Since I’m on Windows, I tried several options for generating the Protobuffers and building/running the go source code: Using Goland, CMD, Cygwin (MinGW), Powershell.
  3. I have tried editing the first import to use rather than the github import as it is deprecated from what I read online.
  4. I did a go get for each missing import. They went into $GOROOT/pkg/mod though instead of $GOROOT/src and I couldn’t reach them, so I manually moved them to src. I then edited the auto-generated .go files from protoc to look like:
    proto "protobuf/proto"
    protoreflect "protobuf/reflect/protoreflect"
    protoimpl "protobuf/runtime/protoimpl"

    And this actually worked… although ALL imports in those files are also reading from so… I now have like 50 import errors… Plus I don’t really want to have to edit generated protobuf files every time I rebuild them.

  5. I tried resolving dependencies using go mod init protobuf_example in my root dir and then I tried go mod tidy which looked like it did "stuff" (here’s a snippet):
    go: found in v1.25.0
    go: found in v1.25.0
    go: found in v1.25.0
    go: found in v1.25.0
    go: found in v1.25.0
    go: found in v1.25.0

But in the end it didn’t solve anything. I now have a go.mod file which contains require(*repos*) and when I hover over them in Goland it just says ‘unresolved dependency’

I’m quite new to Go and its arcane package management system. Coming from Python and Pip, this is quite a headache.


As requested, I’m adding some extra info:

My go.mod is:

module protobuf_prac

go 1.16

require ( v1.4.3 v0.5.5 // indirect v1.25.0

I changed my imports to be non-relative and to use the correct module as such:

package main
import (

func main() {
        city := location.City{Name: "Sofia", ZipCode: "1000", CountryName: "Bulgaria"}

The issue persists.


As mentioned by @s0xzwasd the issue was disabled Go modules in Goland.

Answered By – zkscpqm

Answer Checked By – Clifford M. (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.