16

I have simple proto file with following content.

syntax="proto3";

package main;

message Person {
      string name = 1;
      int32 age = 2; 
}

I am trying to generate go code for it using protoc. I run:

protoc --go_out=. simple.proto

I receive following error:

protoc-gen-go: unable to determine Go import path for "simple.proto"

Please specify either:
        • a "go_package" option in the .proto source file, or
        • a "M" argument on the command line.

main.go, go.mod and simple.proto is in the same folder. Both protoc and protoc-gen-go are defined in PATH enviroement.

srbemr
  • 318
  • 1
  • 2
  • 8
  • Does this answer your question? [Correct format of protoc go\_package?](https://stackoverflow.com/questions/61666805/correct-format-of-protoc-go-package) – blackgreen Jan 05 '22 at 07:53
  • see this answer https://stackoverflow.com/a/68969208/2270041 – Matteo Jan 07 '22 at 07:58

5 Answers5

15

You forgot to linkedlist the file with it by adding:

option go_package = "./";

You need to linkedlist it first to make it work. It was same issues here

14

Protoc requires that the package be specified, then the solution is to add

option go_package = "./your-package-name";

to make your file looks like the following:

syntax="proto3";

package main;

option go_package = "./your-package-name";

message Person {
      string name = 1;
      int32 age = 2; 
}

then you can run the command e.g:

protoc -I src/ --go_out=src/ src/simple/simple.proto

where --go_out=src/ specifies where your file will be generated then the relative path to your proto file.

Note: Don't forget to prefix the option go_package with ./

Oscar Gallardo
  • 2,240
  • 3
  • 27
  • 47
9

You are missing option go_package.

The name you will give to option go_package will be the name of the package that will be generated by the protoc. By doing so, you can import thus access message fields.

blockByblock
  • 366
  • 2
  • 4
  • 14
0

I have a similar problem.

I thought protocol buffers were supposed to be language neutral. If we are adding go_package to the proto files, then if we try to compile these proto files to a different language, we will have to make changes to the files.

The solutions I have seen works if you are looking at generating only go files.

Future Man
  • 43
  • 2
  • 8
  • 2
    Hi. Thanks for your first answer! I think this probably fits better as a comment to the question than an answer, because you don't actually give a solution. You can add comments to questions just below the question itself. – Kevin Buchs May 06 '22 at 21:41
-2

first make sure you installed compiler correctly

sudo apt install protobuf-compiler
sudo apt install golang-goprotobuf-dev

use this command

protoc -I=src/ --go_out=src/ src/simple.proto

-I = IPATH -Specify the directory in which to search for imports
--go_out= output directory