What is the purpose of the go_package_prefix in the buf build library?

374 views Asked by At

I I don't understand the purpose of the go_package_prefix code snippet in generating protobuf files

version: v1
managed:
  enabled: true
  go_package_prefix:
    default: github.com/imsobad/grpc-gateway-demo
    except:
      - buf.build/googleapis/googleapis
plugins:
  - plugin: buf.build/protocolbuffers/go:v1.30.0
    out: gen/go
    opt:
      - paths=source_relative
  - plugin: buf.build/grpc/go:v1.3.0
    out: gen/go
    opt:
      - paths=source_relative
      - require_unimplemented_servers=false
  - plugin: grpc-gateway
    out: gen/go
    opt:
      - paths=source_relative
      - generate_unbound_methods=true

I want to understand how the command "go_package_prefix" work

1

There are 1 answers

0
skyzip On

For anyone still interested, according to the docs: https://buf.build/docs/generate/managed-mode/ managed mode helps you, or other developers manage .proto files in your "buf" project, instead of managing them individually. The docs should explain that better than me though. So read that. Really. It is nicely documented.

However I can provide you with an example.

Lets say you have a .proto file, like this:

syntax = "proto3";

package v1.model;

message SomeMessage {
  string foo = 1;
  uint32 bar = 2;
}

Notice the package specification, and the lack of go_package option.

When using managed mode with default prefix, your package is essentially prefixed with your specified prefix option. E.g. you could simply specify com.your.domain/project as go_package_prefix default and then, all of your compiled proto potentially relying on these definitions will have com.your.domain/project as a prefix in their imports (no need to specify go_package for every individual protofile).

// Code generated by protoc-gen-go. DO NOT EDIT.
// ...

import(
  // ...
  model "com.your.domain/project/v1/model"
  // instead of just being `model v1/model` which is usually not what you would want
  // ...
)
// 

Hopefully, this explains some of the purpose behind the go_package_prefix option.

Note: I am not a native speaker, so in case my wording or explanation is off-throwing, please feel free to correct me.