This package provides Go structures for definitions used in Protocol Buffer file and a helper to write those constructs to .proto
file with valid syntax.
-
Get the package
go get github.com/xapi-tools/protodefgen@latest
-
Create Go structures for Protocol Buffers and generate file
package main import pfg "github.com/xapi-tools/protodefgen" func main() { pw := pfg.NewProtoDefWriter( &pfg.Proto{ Package: "test", Imports: []string{"google/protobuf/empty.proto"}, Messages: []pfg.Message{ { Name: "BasicType", Description: "This message contains basic types", Fields: []pfg.MessageField{ { Description: "This is required string field", Name: "name", Type: "string", Id: 0, }, }, }, }, Services: []pfg.Service{ { Name: "BasicService", Description: "This is a service exercising BasicType", Methods: []pfg.ServiceMethod{ { Name: "GetBasic", Description: "Get BasicType", Request: "google.protobuf.Empty", Response: "BasicType", }, }, }, }, }, &pfg.ProtoWriterOpts{ IndentWidth: 4, }, ) pw.ToFile("./test.proto") }
Check following for more comprehensive examples: