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 "github.com/golang/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/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:
google.golang.org/protobuf/proto
rather than the github import as it is deprecated from what I read online.go get google.golang.org/protobuf/xxxx
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 google.golang.org
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.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 google.golang.org/protobuf/encoding/protowire in google.golang.org/protobuf v1.25.0
go: found google.golang.org/protobuf/internal/encoding/messageset in google.golang.org/protobuf v1.25.0
go: found google.golang.org/protobuf/internal/errors in google.golang.org/protobuf v1.25.0
go: found google.golang.org/protobuf/internal/fieldsort in google.golang.org/protobuf v1.25.0
go: found google.golang.org/protobuf/internal/flags in google.golang.org/protobuf v1.25.0
go: found google.golang.org/protobuf/internal/genid in google.golang.org/protobuf 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 (
github.com/golang/protobuf v1.4.3
github.com/google/go-cmp v0.5.5 // indirect
google.golang.org/protobuf v1.25.0
)
I changed my imports to be non-relative and to use the correct module as such:
package main
import (
"fmt"
"protobuf_prac/proto/out/loc/location"
)
func main() {
city := location.City{Name: "Sofia", ZipCode: "1000", CountryName: "Bulgaria"}
fmt.Println(city.String())
}
The issue persists.
As mentioned by @s0xzwasd the issue was disabled Go modules in Goland.