1

I am import two different proto files in my current proto file as below

import "author/message/name.proto"
import "reader/message/details.proto"

in name.proto I have go_package = "author/message" and in details.proto I have go_package = "reader/message" because of which when pb.go is generated import alias is showing as below

message1 "author/message"

message2 "reader/message"

I am trying to find a way where i can name alias like authormsg and readermsg respectively during pb.go generation. I have checked documentation but couldnt find anyway to do that.

BhanuReddy
  • 74
  • 10

1 Answers1

1

You can add the explicit Go package name like this:

  • name.proto: option go_package="author/message;authormsg"
  • details.proto: option go_package="reader/message;readermsg"

Means you add the package name separated with a semicolon (;) after the import path.

Note:

This usage is discouraged since the package name will be derived by default from the import path in a reasonable manner. Protocol Buffers documentation

Dominik
  • 2,283
  • 1
  • 25
  • 37
  • I have tried this solution but still in pb.go it is generating package alias names as message1 & message2. Even though I have added `option go_package="author/message;authormsg"` `option go_package="reader/message;readermsg"` – BhanuReddy Jun 03 '21 at 07:02
  • @BhanuReddy Ah, you don't mean the Go package name, e.g. `package authormsg` in `name.pb.go`. Instead do you want that _inside_ your `pb.go` there's no `import message1 "author/message"`? That might be when your `name.pb.go` really resides in path `author/message` and `protoc-gen-go` assumes the [Go's convention import-path base == package-name](https://blog.golang.org/package-names#TOC_4.) and doesn't check the real `package` inside the `pb.go`. However, why do you care about this? When using the generated code, you don't see it at all. – Dominik Jun 03 '21 at 10:14
  • yeah in import statement package alias is not happening like import ( MY_NAME "reader/message" ) since there are multiple package imports like below `import( message1 "reader/message" message2 "writer/message" message3 "fastreader/message" message4 "fastwriter/message" )` The package alias names message1 ... message4 while reading code it is getting difficult so wanted to make it more readable by generating pb.go like below `import( reader "reader/message" writer "writer/message" fastreader "fastreader/message" fastwriter "fastwriter/message" )` – BhanuReddy Jun 04 '21 at 05:34