Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How correctly import protobuf's files?

Inside city.proto file I want to use (import) protobuf's .proto files. In my golang application I use go modules.

city.proto:

syntax = "proto3";

package proto;

import "google/protobuf/timestamp.proto";

option go_package = "./proto";

message City {
    google.protobuf.Timestamp create_at = 1;
}

When I try to generate code from city.proto file it raise such error:

google/protobuf/timestamp.proto: File not found.
city.proto:3:1: Import "google/protobuf/timestamp.proto" was not found or had errors.
city.proto:25:5: "google.protobuf.Timestamp" is not defined.

I created a proto folder in the directory of my gRPC project. The city.proto file is located in this folder. I run such command:

protoc -I proto/ proto/city.proto --go_out=plugins=grpc:proto/city.

This command works only in cases when I don't use import in the proto file.

go version:

go version go1.12.9 windows/amd64

protoc --version:

libprotoc 3.11.4

echo %GOPATH%:

C:\Users\NNogerbek\go

Inside that directory I see three folders with such structure:

bin
    protoc.exe
    protoc-gen-go.exe
pkg
    mod
        **packages**
src
    google.com
        protobuf
            timestamp.proto

I run such command:

go list -f "{{ .Path }} {{ .Dir }}" -m github.com/golang/protobuf

Command result:

github.com/golang/protobuf C:\Users\NNogerbek\go\pkg\mod\github.com\golang\[email protected]
like image 872
Nurzhan Nogerbek Avatar asked Oct 21 '25 04:10

Nurzhan Nogerbek


1 Answers

After numerous attempts, I found a way to generate the go code to my gRPC server without errors using the following command:

protoc -I. -I%GOPATH%/src --gogofaster_out=plugins=grpc:. proto/city.proto

As you can see in the command, I specify the path to the src folder where the protobuf's files are located.

Also I used gogofaster plugin of gogoprotobuf package.

like image 64
Nurzhan Nogerbek Avatar answered Oct 24 '25 15:10

Nurzhan Nogerbek