Skip to content
Inject custom tags to protobuf golang struct
Go Shell
Branch: master
Clone or download
favadi Merge pull request #28 from msolimans/master
XXX skip fields sample with unit test
Latest commit c2c1884 Oct 8, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
pb XXX fields sample with unit tests Oct 7, 2018
vendor add vendor Aug 2, 2018
.gitignore ignore binary file Apr 6, 2016
.travis.yml enabled goveralls May 21, 2018
LICENSE Initial commit Apr 6, 2016
file.go skipped if field.Names are empty Oct 3, 2018
go.mod add vendor Aug 2, 2018
go.sum add vendor Aug 2, 2018
parse.go preserve spaces in tag values May 25, 2018


Build Status Go Report Card Coverage Status


Golang protobuf doesn't support custom tags to generated structs. This script injects custom tags to generated protobuf files, useful for things like validation struct tags.


  • protobuf version 3

    For OS X:

    brew install protobuf
  • go support for protobuf: go get -u{proto,protoc-gen-go}

  • go get or download the binaries from releases page.


Add a comment with syntax // @inject_tag: custom_tag:"custom_value" before fields to add custom tag to in .proto files.


// file: test.proto
syntax = "proto3";

package pb;

message IP {
  // @inject_tag: valid:"ip"
  string Address = 1;

Generate with protoc command as normal.

protoc --go_out=. test.proto

Run protoc-go-inject-tag with generated file test.pb.go.

protoc-go-inject-tag -input=./test.pb.go

The custom tags will be injected to test.pb.go.

type IP struct {
	// @inject_tag: valid:"ip"
	Address string `protobuf:"bytes,1,opt,name=Address,json=address" json:"Address,omitempty" valid:"ip"`

To skip the tag for the generated XXX_* fields, use -XXX_skip=yaml,xml flag.

You can’t perform that action at this time.