No description
  • Go 97.7%
  • Makefile 2.3%
Find a file
oss-core-libraries-dashboard[bot] d1df1a71f3
[COMPLIANCE] Update Copyright and License Headers (#5)
Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2026-02-20 15:28:53 +05:30
e2e Update to use google.golang.org/protobuf (#1) 2023-01-05 16:15:59 -05:00
gen Update to use google.golang.org/protobuf (#1) 2023-01-05 16:15:59 -05:00
.gitignore Update to use google.golang.org/protobuf (#1) 2023-01-05 16:15:59 -05:00
go.mod Update to use google.golang.org/protobuf (#1) 2023-01-05 16:15:59 -05:00
go.sum Update to use google.golang.org/protobuf (#1) 2023-01-05 16:15:59 -05:00
LICENSE [COMPLIANCE] Update Copyright and License Headers (#5) 2026-02-20 15:28:53 +05:30
main.go Update to use google.golang.org/protobuf (#1) 2023-01-05 16:15:59 -05:00
Makefile Update to use google.golang.org/protobuf (#1) 2023-01-05 16:15:59 -05:00
README.md Update to use google.golang.org/protobuf (#1) 2023-01-05 16:15:59 -05:00

protoc-gen-go-binary

This is a plugin for the Google Protocol Buffers compiler protoc that generates code to implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler by just calling the Marshal and Unmarshal functions already generated for the types.

This enables Go-generated protobuf messages to be used in situations where the code already supports using the binary marshaling interfaces.

The code heavily relies on google.golang.org/protobuf/compiler/protogen and is mostly boilerplate.

Install

go get github.com/hashicorp/protoc-gen-go-binary

Also required:

Usage

Define your messages like normal:

syntax = "proto3";

message Request {
  oneof kind {
    string name = 1;
    int32  code = 2;
  }
}

The example message purposely uses a oneof since this won't work by default with encoding/json. Next, generate the code:

protoc --go_out=. --go-binary_out=. request.proto

Your output should contain a file request.pb.binary.go which contains the implementation of encoding.BinaryMarshal/BinaryUnmarshal for all your message types. You can then encode binary encode your message as protobufs.

import (
  "bytes"
  "encoding/gob"
)

var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)

// Marshal
err := encoder.Encode(&Request{
  Kind: &Kind_Name{
    Name: "alice",
  },
}

// Unmarshal
var result Request
decoder := gob.NewDecoder(&buf)
err := decoder.Decode(&result)