0

Is there any chance to avoid code duplication on Go in .proto and .go files when using gRPC?

We need to store both two versions of the code - generated and source files: https://github.com/cqfn/degitx/blob/master/proto/hook.proto https://github.com/cqfn/degitx/blob/master/proto/go/degitxpb/hook.pb.go

On Java everything is easy - you have abstract classes and do not store them in repo. You simply do implementation of it. And on Go this looks to be a problem that causes a code duplication. Has someone solved it?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Do you want to change the export path ?! – ttrasn Oct 06 '20 at 09:26
  • 2
    Java also needs the proto files to be compiled into java. Whether you store the generated files in the repo or generate them on the fly as part of your CI system is up to you, but storing them is generally more reliable as you can ensure that differences in tooling (eg: protoc version) get caught. Even then, you failed to mention why this is a problem. – Marc Oct 06 '20 at 11:17
  • 3
    there is no code duplication - your `proto` is the source of truth for you compiled `go` file. – Oleg Butuzov Oct 06 '20 at 11:59
  • @Marc and Oleg, yes we understand that all proto files are generating .go file, but unfortunately it generates the code that should be changed. And in this case you are not able simply to use the generated code, you need to change it manually first. Because of this these generated files should be stored in the repository. – Andrew Kuleshov Oct 06 '20 at 13:02
  • 4
    Why are you modifying generates files? You can already use plugins or add code in the same package as the generates files. You **should not** be modifying the generated `.pb.go` files. – Marc Oct 06 '20 at 13:39
  • 1
    As @Marc said you should not modify your stub files ```(.pb.go)```. You import everything you need from the stub and work with it. When you want to update, you just replace the stub. There is no need to manually change anything. – dclipca Oct 07 '20 at 19:17

0 Answers0